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

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

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

import { Request, NextFunction, Response } from 'express';
import { AuthController } from '../../src/api/controller/authController';
import { Request as JestRequest } from 'jest-express/lib/request';
import { Response as JestResponse } from 'jest-express/lib/response';

describe('LoginUser', () => {
  it('should successfully login the user', async () => {
    const dataObj = {
      email: 'aarushi@studiographene.com',
      password: 'Swaraj07@',
    };
    const mockRequest = {
      body: dataObj,
    } as Request;
    const mockResponse = new JestResponse();
    new AuthController().login(
      (mockRequest as unknown) as Request,
      (mockResponse as unknown) as Response
    ).then(response => {
      console.log("response for test", response);
      expect(response.body.message).toEqual('Successfully Logged In');
    })
  });

  it('should not login the user', async () => {
    const dataObj = {
      email: 'aarushi25@studiographene.com',
      password: 'Swaraj07@',
    };
    const mockRequest = {
      body: dataObj,
    } as Request;
    const mockResponse = new JestResponse();
    new AuthController().login(
      (mockRequest as unknown) as Request,
      (mockResponse as unknown) as Response
    ).then(response => expect(response.body.message).toEqual('Incorrect username or password.'));
  });
});

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

public async login(logInUserObj: LogInUserObj): Promise<any> {
    console.log("Came here through test 2--");
    const poolData = {
      UserPoolId: AWS_COGNITO_USER_POOL_ID,
      ClientId: AWS_COGNITO_USER_POOL_CLIENT_ID
    };
    const userPool = new CognitoUserPool(poolData);
    const authenticationDetails = new AuthenticationDetails({
      Username: logInUserObj.email,
      Password: logInUserObj.password
    });
    const userData = {
      Username: logInUserObj.email,
      Pool: userPool
    };
    const cognitoUser = new CognitoUser(userData);
    console.log("Came here through test 5--");
    return new Promise((resolve, reject) => {
      console.log("Came here through test 6--");
     return cognitoUser.authenticateUser(authenticationDetails, {
        onSuccess: (result) => {
          const userDetails = {
            access_token: result.getAccessToken().getJwtToken(),
            user_details: result.getIdToken().decodePayload(),
            refresh_token: result.getRefreshToken().getToken(),
            user_id: result.getIdToken().decodePayload().sub
          };
          console.log("Came here through test 3--");
          resolve(userDetails);
        },
        onFailure: (err) => {
          console.log("Came here through test 4--");
          resolve(err.message);
        }
      });
    });
  }

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

暂无答案!

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

相关问题