authenticateuser的单元测试-aws cognito identity js-jest

a5g8bdjr  于 2021-10-10  发布在  Java
关注(0)|答案(0)|浏览(345)

我制作了一个api,供用户通过aws cognito登录。我正在使用jest为同样的问题编写单元测试用例。我不确定那里出了什么问题。我对cognito和单元测试用例都很陌生,也就是说,jest,所以我可以在这里使用帮助。

  1. import { Request, NextFunction, Response } from 'express';
  2. import { AuthController } from '../../src/api/controller/authController';
  3. import { Request as JestRequest } from 'jest-express/lib/request';
  4. import { Response as JestResponse } from 'jest-express/lib/response';
  5. describe('LoginUser', () => {
  6. it('should successfully login the user', async () => {
  7. const dataObj = {
  8. email: 'aarushi@studiographene.com',
  9. password: 'Swaraj07@',
  10. };
  11. const mockRequest = {
  12. body: dataObj,
  13. } as Request;
  14. const mockResponse = new JestResponse();
  15. new AuthController().login(
  16. (mockRequest as unknown) as Request,
  17. (mockResponse as unknown) as Response
  18. ).then(response => {
  19. console.log("response for test", response);
  20. expect(response.body.message).toEqual('Successfully Logged In');
  21. })
  22. });
  23. it('should not login the user', async () => {
  24. const dataObj = {
  25. email: 'aarushi25@studiographene.com',
  26. password: 'Swaraj07@',
  27. };
  28. const mockRequest = {
  29. body: dataObj,
  30. } as Request;
  31. const mockResponse = new JestResponse();
  32. new AuthController().login(
  33. (mockRequest as unknown) as Request,
  34. (mockResponse as unknown) as Response
  35. ).then(response => expect(response.body.message).toEqual('Incorrect username or password.'));
  36. });
  37. });

它通过了两个测试用例,但没有返回承诺,也就是说,没有进入代码的那一部分。
authenticateuser的cognito服务如下:

  1. public async login(logInUserObj: LogInUserObj): Promise<any> {
  2. console.log("Came here through test 2--");
  3. const poolData = {
  4. UserPoolId: AWS_COGNITO_USER_POOL_ID,
  5. ClientId: AWS_COGNITO_USER_POOL_CLIENT_ID
  6. };
  7. const userPool = new CognitoUserPool(poolData);
  8. const authenticationDetails = new AuthenticationDetails({
  9. Username: logInUserObj.email,
  10. Password: logInUserObj.password
  11. });
  12. const userData = {
  13. Username: logInUserObj.email,
  14. Pool: userPool
  15. };
  16. const cognitoUser = new CognitoUser(userData);
  17. console.log("Came here through test 5--");
  18. return new Promise((resolve, reject) => {
  19. console.log("Came here through test 6--");
  20. return cognitoUser.authenticateUser(authenticationDetails, {
  21. onSuccess: (result) => {
  22. const userDetails = {
  23. access_token: result.getAccessToken().getJwtToken(),
  24. user_details: result.getIdToken().decodePayload(),
  25. refresh_token: result.getRefreshToken().getToken(),
  26. user_id: result.getIdToken().decodePayload().sub
  27. };
  28. console.log("Came here through test 3--");
  29. resolve(userDetails);
  30. },
  31. onFailure: (err) => {
  32. console.log("Came here through test 4--");
  33. resolve(err.message);
  34. }
  35. });
  36. });
  37. }

它不会进入cognitouser.authenticateuser函数。我做错了什么?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题