import AuthService from './AuthService' import { ICredentials } from '@aws-amplify/core' import { mocked } from 'ts-jest/utils' // import fetchMock from 'jest-fetch-mock' import Auth from '@aws-amplify/auth' import { CognitoUser, CodeDeliveryDetails, ISignUpResult } from '@remote.it/types' jest.mock('aws-amplify') const mockedAuth = mocked(Auth, true) // Mock the Cognito signIn method. function mockSignIn(response: any): void { mockedAuth.signIn.mockResolvedValueOnce(response as CognitoUser) } describe('AuthService', () => { beforeEach(() => { mockedAuth.signIn.mockReset() }) // describe('.checkSignIn', () => { // xtest('should...', async () => {}) // }) describe('.signIn', () => { test('should return a user if successfully signing in with username and password', async () => { // Setup mockSignIn({ username: 'user@company.com' }) // Run const cognito = new AuthService({ cognitoClientID: '26g0ltne0gr8lk1vs51mihrmig', }) const result = await cognito.signIn('user@company.com', 'supers3kret!') // Assert expect(result.cognitoUser).not.toBeUndefined() expect(result.cognitoUser).not.toBeUndefined() // Removed get remoteit user (added unneded delay to login) // expect(user?.remoteitUser?.partnerPortalAccess).toBe(false) expect(result.cognitoUser?.challengeName).toBeUndefined() }) test('should give a two-factor user a challenge response', async () => { // Setup mockSignIn({ username: 'user@company.com', challengeName: 'SMS_MFA', }) // Run const cognito = new AuthService({ cognitoClientID: '26g0ltne0gr8lk1vs51mihrmig', }) const result = await cognito.signIn('user@company.com', 'supers3kret!') // Assert expect(result.cognitoUser?.challengeName).toBe('SMS_MFA') expect(result.cognitoUser?.challengeName).toBe('SMS_MFA') }) test('should set authProvider for a Google Auth user', async () => { // Setup mockSignIn({ username: 'Google-user-1234' }) // Run const cognito = new AuthService({ cognitoClientID: '26g0ltne0gr8lk1vs51mihrmig', }) const result = await cognito.signIn('user@company.com', 'supers3kret!') // Assert expect(result.cognitoUser?.authProvider).toBe('Google') }) test('should set partner portal access if the user has access', async () => { // Setup mockSignIn({ username: 'user@company.com' }) // Run const cognito = new AuthService({}) const result = await cognito.signIn('user@company.com', 'supers3kret!') // Assert expect(result.cognitoUser).not.toBeUndefined() // Removed this check for now. Getting user during login slowed down login // expect(user?.remoteitUser?.partnerPortalAccess).toBe(true) }) }) // describe('.confirmSignIn', () => { // xtest('should...', async () => {}) // }) describe('.googleSignIn', () => { afterEach(() => mockedAuth.federatedSignIn.mockReset()) test('should call federatedSignIn with proper params', async () => { // Setup const expected = { accessKeyId: 'some-access-key-id', sessionToken: 'some-session-token', secretAccessKey: 'some-secret-access-key', identityId: 'some-identity-id', authenticated: true, } mockedAuth.federatedSignIn.mockResolvedValueOnce(expected as ICredentials) // Run const cognito = new AuthService({ cognitoClientID: '26g0ltne0gr8lk1vs51mihrmig', }) const creds = await cognito.googleSignIn() // Assert expect(mockedAuth.federatedSignIn).toBeCalledWith({ provider: 'Google', }) expect(creds).toEqual(expected) }) }) describe('.signUp', () => { afterEach(() => mockedAuth.federatedSignIn.mockReset()) test('should call signUp with proper params', async () => { // Setup const email = 'some@email.com' const password = 'supers3kret!' const expected = { user: { id: 'some-id', }, userConfirmed: false, userSub: 'some-user-sub', codeDeliveryDetails: { AttributeName: 'some-attribute-name', DeliveryMedium: 'some-delivery-medium', Destination: 'some-destination', }, } mockedAuth.signUp.mockResolvedValueOnce((expected as unknown) as ISignUpResult) // Run const cognito = new AuthService({ cognitoClientID: '26g0ltne0gr8lk1vs51mihrmig', }) const resp = await cognito.signUp(email, password) // Assert expect(mockedAuth.signUp).toBeCalledWith({ username: email, password }) expect(resp).toEqual({ cognitoUser: { ...expected.user, username: email, preferredMFA: 'NOMFA', }, remoteitUser: undefined, }) }) }) describe('.forgotPassword', () => { afterEach(() => mockedAuth.federatedSignIn.mockReset()) test('should call forgotPassword with proper params', async () => { // Setup const email = 'some@email.com' const expected = { AttributeName: 'some-attribute-name', DeliveryMedium: 'some-delivery-medium', Destination: 'some-destination', } mockedAuth.forgotPassword.mockResolvedValueOnce(expected as CodeDeliveryDetails) // Run const cognito = new AuthService({ cognitoClientID: '26g0ltne0gr8lk1vs51mihrmig', }) const resp = await cognito.forgotPassword(email) // Assert expect(mockedAuth.forgotPassword).toBeCalledWith(email) expect(resp).toEqual(expected) }) }) describe('.forgotPasswordSubmit', () => { afterEach(() => mockedAuth.federatedSignIn.mockReset()) test('should call forgotPasswordSubmit with proper params', async () => { // Setup const email = 'some@email.com' const shortcode = '123456' const password = 'supers3kret!' // mockedAuth.forgotPasswordSubmit.mockResolvedValueOnce(Promise.resolve()) // Run const cognito = new AuthService({ cognitoClientID: '26g0ltne0gr8lk1vs51mihrmig', }) await cognito.forgotPasswordSubmit(shortcode, password, email) // Assert expect(mockedAuth.forgotPasswordSubmit).toBeCalledWith(email, shortcode, password) }) }) describe('.requestAccountRecovery', () => { afterEach(() => mockedAuth.federatedSignIn.mockReset()) // xtest('should return user and recovery type if successful', async () => {}) test('should throw if no email provided or available', async () => { const cognito = new AuthService({ cognitoClientID: '26g0ltne0gr8lk1vs51mihrmig', }) const result = await cognito.requestAccountRecovery() expect(result.error === new Error('Cannot request account recovery, no email provided!')) // await expect(cognito.requestAccountRecovery()).rejects.toThrow( // 'Cannot request account recovery, no email provided!' // ) // // Setup // const email = 'some@email.com' // const shortcode = '123456' // const password = 'supers3kret!' // mockedAuth.forgotPasswordSubmit.mockResolvedValueOnce(Promise.resolve()) // // Run // const cognito = new AuthService() // await cognito.forgotPasswordSubmit(email, shortcode, password) // // Assert // expect(mockedAuth.forgotPasswordSubmit).toBeCalledWith( // email, // shortcode, // password // ) }) // xtest('should throw if sign in does not return a user', async () => {}) // xtest('should throw if no challenge code provided', async () => {}) // xtest('should throw if invalid recovery type returned', async () => {}) }) // describe('.verifyRecoveryCode', () => { // xtest('should...', async () => {}) // }) describe('.signOut', () => { afterEach(() => { mockedAuth.signOut.mockReset() }) test('should sign user out of both Cognito and remote.it', async () => { const cognito = new AuthService({ cognitoClientID: '26g0ltne0gr8lk1vs51mihrmig', }) mockedAuth.signOut.mockResolvedValueOnce(Promise.resolve()) await expect(cognito.signOut()).resolves.not.toThrow() }) }) })