|
| 1 | +import linkGithubIdentity from '../linkGithubIdentity'; |
| 2 | +import { |
| 3 | + linkGithub, |
| 4 | + saveCredentialForCurrentUser, |
| 5 | +} from '../../clients/firebase'; |
| 6 | +import {getProfileForAuthenticatedUser} from '../../clients/github'; |
| 7 | +import {bugsnagClient} from '../../util/bugsnag'; |
| 8 | + |
| 9 | +import { |
| 10 | + credentialFactory, |
| 11 | + credentialInUseErrorFactory, |
| 12 | + firebaseErrorFactory, |
| 13 | + userFactory, |
| 14 | +} from '@factories/clients/firebase'; |
| 15 | + |
| 16 | +import {githubProfileFactory} from '@factories/clients/github'; |
| 17 | + |
| 18 | +jest.mock('../../clients/firebase.js'); |
| 19 | +jest.mock('../../clients/github.js'); |
| 20 | +jest.mock('../../util/bugsnag.js'); |
| 21 | + |
| 22 | +describe('linkGithubIdentity', () => { |
| 23 | + test('success', async() => { |
| 24 | + const mockCredential = credentialFactory.build(); |
| 25 | + const mockUser = userFactory.build(); |
| 26 | + |
| 27 | + linkGithub.mockResolvedValue({ |
| 28 | + user: mockUser, |
| 29 | + credential: mockCredential, |
| 30 | + }); |
| 31 | + |
| 32 | + const { |
| 33 | + type, |
| 34 | + payload: { |
| 35 | + credential: {providerId}, |
| 36 | + user, |
| 37 | + }, |
| 38 | + } = await linkGithubIdentity.process(); |
| 39 | + |
| 40 | + expect(linkGithub).toHaveBeenCalledWith(); |
| 41 | + expect(saveCredentialForCurrentUser).toHaveBeenCalledWith(mockCredential); |
| 42 | + expect(type).toBe('IDENTITY_LINKED'); |
| 43 | + expect(providerId).toBe(mockCredential.providerId); |
| 44 | + expect(user).toEqual(mockUser); |
| 45 | + }); |
| 46 | + |
| 47 | + test('credential already in use', async() => { |
| 48 | + const error = credentialInUseErrorFactory.build(); |
| 49 | + const githubProfile = githubProfileFactory.build(); |
| 50 | + |
| 51 | + linkGithub.mockRejectedValue(error); |
| 52 | + getProfileForAuthenticatedUser.mockResolvedValue(githubProfile); |
| 53 | + |
| 54 | + const { |
| 55 | + type, |
| 56 | + payload: { |
| 57 | + credential: { |
| 58 | + providerId, |
| 59 | + accessToken, |
| 60 | + }, |
| 61 | + }, |
| 62 | + } = await linkGithubIdentity.process(); |
| 63 | + expect(linkGithub).toHaveBeenCalledWith(); |
| 64 | + expect(getProfileForAuthenticatedUser).toHaveBeenCalledWith( |
| 65 | + error.credential.accessToken, |
| 66 | + ); |
| 67 | + expect(type).toBe('ACCOUNT_MIGRATION_NEEDED'); |
| 68 | + expect(providerId).toBe(error.credential.providerId); |
| 69 | + expect(accessToken).toBe(error.credential.accessToken); |
| 70 | + }); |
| 71 | + |
| 72 | + test('other error', async() => { |
| 73 | + const otherError = firebaseErrorFactory.build(); |
| 74 | + |
| 75 | + linkGithub.mockRejectedValue(otherError); |
| 76 | + bugsnagClient.notify.mockResolvedValue(); |
| 77 | + |
| 78 | + const { |
| 79 | + type, |
| 80 | + error, |
| 81 | + payload: {message}, |
| 82 | + } = await linkGithubIdentity.process(); |
| 83 | + expect(linkGithub).toHaveBeenCalledWith(); |
| 84 | + expect(bugsnagClient.notify).toHaveBeenCalledWith(otherError); |
| 85 | + expect(type).toBe('LINK_IDENTITY_FAILED'); |
| 86 | + expect(error).toBe(true); |
| 87 | + expect(message).toBe(otherError.code); |
| 88 | + }); |
| 89 | +}); |
0 commit comments