Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.6 kB
0
Indexable
Never
describe('/v2/admin/user/password', () => {
  let user: Request;
  beforeEach(() => {
    requestClear();
    user = requestAuthRegister(
      'john.doe@gmail.com',
      'password1',
      'John',
      'Doe'
    );
  });

  test.each([
    { invalidToken: 2 },
    { invalidToken: undefined },
    { invalidToken: true },
    { invalidToken: null },
  ])('Token is not a valid structure', ({ invalidToken }) => {
    expect(requestUserPasswordUpdate3(invalidToken, 'password1', 'NewPassword1').body).toStrictEqual({
      error: 'Token is not a valid structure',
    });
    expect(requestUserPasswordUpdate3(invalidToken, 'password1', 'NewPassword1').statusCode).toStrictEqual(401);
  });

  test('Provided token is a valid structure, but is not for a currently logged in session', () => {
    expect(requestUserPasswordUpdate3(JSON.stringify({ userId: 1, sessionId: 'string' }), 'password1', 'NewPassword1').body
    ).toStrictEqual({
      error: 'Token is not logged in',
    });
    expect(requestUserPasswordUpdate3(JSON.stringify({ userId: 1, sessionId: 'string' }), 'password1', 'NewPassword1').statusCode).toStrictEqual(403);
  });

  test('Old Password is not the correct old password', () => {
    expect(requestUserPasswordUpdate3(user.token, 'WrongPassword1', 'NewPassword1').body).toStrictEqual({
      error: 'Old Password is not the correct old password',
    });
    expect(requestUserPasswordUpdate3(user.token, 'WrongPassword1', 'NewPassword1').statusCode).toStrictEqual(400);
  });

  test('New Password has already been used before by this user', () => {
    expect(requestUserPasswordUpdate3(user.token, 'password1', 'password1').body).toStrictEqual({
      error: 'New Password has already been used before by this user',
    });
    expect(requestUserPasswordUpdate3(user.token, 'password1', 'password1').statusCode).toStrictEqual(400);

    expect(requestUserPasswordUpdate3(user.token, 'password1', 'newpassword1').body).toStrictEqual({});

    expect(requestUserPasswordUpdate3(user.token, 'newpassword1', 'password1').body).toStrictEqual({
      error: 'New Password has already been used before by this user',
    });
    expect(requestUserPasswordUpdate3(user.token, 'newpassword1', 'password1').statusCode).toStrictEqual(400);
  });

  test('New Password is too short', () => {
    expect(requestUserPasswordUpdate3(user.token, 'password1', 'short1').body).toStrictEqual({
      error: 'New Password must be at least 8 characters',
    });
    expect(requestUserPasswordUpdate3(user.token, 'password1', 'short1').statusCode).toStrictEqual(400);
  });

  test.each([
    { newPassword: 'allletters' },
    { newPassword: '1234567890' }
  ])('New Password does not contain at least one number and at least one letter', ({ newPassword }) => {
    expect(requestUserPasswordUpdate3(user.token, 'password1', newPassword).body).toStrictEqual({
      error: 'New Password does not contain at least one number and at least one letter',
    });
    expect(requestUserPasswordUpdate3(user.token, 'password1', newPassword).statusCode).toStrictEqual(400);
  });

  test('Succsessful password change', () => {
    const responseUpdatePassword: Request = requestUserPasswordUpdate3(user.token, 'password1', 'newPassword1');
    expect(responseUpdatePassword.body).toStrictEqual({});
    expect(responseUpdatePassword.statusCode).toStrictEqual(200);
    expect(requestUserPasswordUpdate3(user.token, 'newPassword1', 'password1').body).toStrictEqual({
      error: 'New Password has already been used before by this user',
    });
  });
});