|
| 1 | +import { JiraAuthentictor } from './jiraAuthenticator'; |
| 2 | +import { OAuthProvider, AccessibleResource, ProductJira } from './authInfo'; |
| 3 | +import { CredentialManager } from './authStore'; |
| 4 | +import { Container } from '../container'; |
| 5 | + |
| 6 | +jest.mock('./authStore'); |
| 7 | +jest.mock('../container', () => ({ |
| 8 | + Container: { |
| 9 | + clientManager: { |
| 10 | + jiraClient: jest.fn(), |
| 11 | + }, |
| 12 | + }, |
| 13 | +})); |
| 14 | + |
| 15 | +describe('JiraAuthenticator', () => { |
| 16 | + it('should return site details for given resources', async () => { |
| 17 | + const provider = OAuthProvider.JiraCloud; |
| 18 | + const userId = 'user123'; |
| 19 | + const resources: AccessibleResource[] = [ |
| 20 | + { |
| 21 | + id: 'resource1', |
| 22 | + name: 'Resource 1', |
| 23 | + url: 'https://resource1.atlassian.net', |
| 24 | + avatarUrl: 'https://resource1.atlassian.net/avatar.png', |
| 25 | + scopes: [], |
| 26 | + }, |
| 27 | + ]; |
| 28 | + |
| 29 | + const mockClient = { |
| 30 | + getFields: jest.fn().mockResolvedValue([{ id: 'resolution' }]), |
| 31 | + }; |
| 32 | + |
| 33 | + (Container.clientManager.jiraClient as jest.Mock).mockResolvedValue(mockClient); |
| 34 | + (CredentialManager.generateCredentialId as jest.Mock).mockReturnValue('credential123'); |
| 35 | + |
| 36 | + const authenticator = new JiraAuthentictor(); |
| 37 | + const result = await authenticator.getOAuthSiteDetails(provider, userId, resources); |
| 38 | + |
| 39 | + expect(result).toEqual([ |
| 40 | + { |
| 41 | + avatarUrl: 'https://resource1.atlassian.net/avatar.png', |
| 42 | + baseApiUrl: 'https://api.atlassian.com/ex/jira/resource1/rest', |
| 43 | + baseLinkUrl: 'https://resource1.atlassian.net', |
| 44 | + host: 'resource1.atlassian.net', |
| 45 | + id: 'resource1', |
| 46 | + name: 'Resource 1', |
| 47 | + product: ProductJira, |
| 48 | + isCloud: true, |
| 49 | + userId: 'user123', |
| 50 | + credentialId: 'credential123', |
| 51 | + hasResolutionField: true, |
| 52 | + }, |
| 53 | + ]); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should handle resources without resolution field', async () => { |
| 57 | + const provider = OAuthProvider.JiraCloud; |
| 58 | + const userId = 'user123'; |
| 59 | + const resources: AccessibleResource[] = [ |
| 60 | + { |
| 61 | + id: 'resource2', |
| 62 | + name: 'Resource 2', |
| 63 | + url: 'https://resource2.atlassian.net', |
| 64 | + avatarUrl: 'https://resource2.atlassian.net/avatar.png', |
| 65 | + scopes: [], |
| 66 | + }, |
| 67 | + ]; |
| 68 | + |
| 69 | + const mockClient = { |
| 70 | + getFields: jest.fn().mockResolvedValue([{ id: 'someOtherField' }]), |
| 71 | + }; |
| 72 | + |
| 73 | + (Container.clientManager.jiraClient as jest.Mock).mockResolvedValue(mockClient); |
| 74 | + (CredentialManager.generateCredentialId as jest.Mock).mockReturnValue('credential456'); |
| 75 | + |
| 76 | + const authenticator = new JiraAuthentictor(); |
| 77 | + const result = await authenticator.getOAuthSiteDetails(provider, userId, resources); |
| 78 | + |
| 79 | + expect(result).toEqual([ |
| 80 | + { |
| 81 | + avatarUrl: 'https://resource2.atlassian.net/avatar.png', |
| 82 | + baseApiUrl: 'https://api.atlassian.com/ex/jira/resource2/rest', |
| 83 | + baseLinkUrl: 'https://resource2.atlassian.net', |
| 84 | + host: 'resource2.atlassian.net', |
| 85 | + id: 'resource2', |
| 86 | + name: 'Resource 2', |
| 87 | + product: ProductJira, |
| 88 | + isCloud: true, |
| 89 | + userId: 'user123', |
| 90 | + credentialId: 'credential456', |
| 91 | + hasResolutionField: false, |
| 92 | + }, |
| 93 | + ]); |
| 94 | + }); |
| 95 | +}); |
0 commit comments