|
| 1 | +import GitHub from './github'; |
| 2 | +import { getRemoteUrls } from '../util/create-git-meta'; |
| 3 | +import { repositoriesQuery, userConnectionsQuery } from '../graphql'; |
| 4 | +import BaseClass from './base-class'; |
| 5 | +import { existsSync } from 'fs'; |
| 6 | + |
| 7 | +jest.mock('../util/create-git-meta'); |
| 8 | +jest.mock('fs', () => ({ |
| 9 | + ...jest.requireActual('fs'), |
| 10 | + existsSync: jest.fn(), |
| 11 | +})); |
| 12 | + |
| 13 | +const userConnections = [ |
| 14 | + { |
| 15 | + __typename: 'UserConnection', |
| 16 | + userUid: 'testuser1', |
| 17 | + provider: 'GitHub', |
| 18 | + }, |
| 19 | +]; |
| 20 | +const repositories = [ |
| 21 | + { |
| 22 | + __typename: 'GitRepository', |
| 23 | + id: '495370701', |
| 24 | + url: 'https://github.com/test-user/nextjs-ssr-isr-demo', |
| 25 | + name: 'nextjs-ssr-isr-demo', |
| 26 | + fullName: 'test-user/nextjs-ssr-isr-demo', |
| 27 | + defaultBranch: 'main', |
| 28 | + }, |
| 29 | + { |
| 30 | + __typename: 'GitRepository', |
| 31 | + id: '555341263', |
| 32 | + url: 'https://github.com/test-user/static-site-demo', |
| 33 | + name: 'static-site-demo', |
| 34 | + fullName: 'test-user/static-site-demo', |
| 35 | + defaultBranch: 'main', |
| 36 | + }, |
| 37 | + { |
| 38 | + __typename: 'GitRepository', |
| 39 | + id: '647250661', |
| 40 | + url: 'https://github.com/test-user/eleventy-sample', |
| 41 | + name: 'eleventy-sample', |
| 42 | + fullName: 'test-user/eleventy-sample', |
| 43 | + defaultBranch: 'main', |
| 44 | + }, |
| 45 | +]; |
| 46 | + |
| 47 | +describe('GitHub Adapter', () => { |
| 48 | + let logMock: jest.Mock; |
| 49 | + let exitMock: jest.Mock; |
| 50 | + |
| 51 | + beforeEach(() => { |
| 52 | + logMock = jest.fn(); |
| 53 | + exitMock = jest.fn().mockImplementationOnce(() => { |
| 54 | + throw new Error('1'); |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + afterEach(() => { |
| 59 | + jest.resetAllMocks(); |
| 60 | + }); |
| 61 | + |
| 62 | + describe('checkGitHubConnected', () => { |
| 63 | + it('should return true if GitHub is connected', async () => { |
| 64 | + const userConnectionResponse = { data: { userConnections } }; |
| 65 | + const apolloClient = { |
| 66 | + query: jest.fn().mockResolvedValueOnce(userConnectionResponse), |
| 67 | + } as any; |
| 68 | + const githubAdapterInstance = new GitHub({ |
| 69 | + config: { projectBasePath: '/home/project1', provider: 'GitHub' }, |
| 70 | + apolloClient: apolloClient, |
| 71 | + log: logMock, |
| 72 | + } as any); |
| 73 | + const connectToAdapterOnUiMock = jest |
| 74 | + .spyOn(BaseClass.prototype, 'connectToAdapterOnUi') |
| 75 | + .mockResolvedValueOnce(undefined); |
| 76 | + |
| 77 | + await githubAdapterInstance.checkGitHubConnected(); |
| 78 | + |
| 79 | + expect(apolloClient.query).toHaveBeenCalledWith({ query: userConnectionsQuery }); |
| 80 | + expect(logMock).toHaveBeenCalledWith('GitHub connection identified!', 'info'); |
| 81 | + expect(githubAdapterInstance.config.userConnection).toEqual(userConnections[0]); |
| 82 | + expect(connectToAdapterOnUiMock).not.toHaveBeenCalled(); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should log an error and exit if GitHub is not connected', async () => { |
| 86 | + const userConnectionResponse = { data: { userConnections: [] } }; |
| 87 | + const connectToAdapterOnUiMock = jest.spyOn(BaseClass.prototype, 'connectToAdapterOnUi').mockResolvedValueOnce(); |
| 88 | + const apolloClient = { |
| 89 | + query: jest.fn().mockResolvedValueOnce(userConnectionResponse), |
| 90 | + } as any; |
| 91 | + const githubAdapterInstance = new GitHub({ |
| 92 | + config: { projectBasePath: '/home/project1' }, |
| 93 | + apolloClient: apolloClient, |
| 94 | + log: logMock, |
| 95 | + } as any); |
| 96 | + |
| 97 | + await githubAdapterInstance.checkGitHubConnected(); |
| 98 | + |
| 99 | + expect(apolloClient.query).toHaveBeenCalledWith({ query: userConnectionsQuery }); |
| 100 | + expect(logMock).toHaveBeenCalledWith('GitHub connection not found!', 'error'); |
| 101 | + expect(connectToAdapterOnUiMock).toHaveBeenCalled(); |
| 102 | + expect(githubAdapterInstance.config.userConnection).toEqual(undefined); |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + describe('checkGitRemoteAvailableAndValid', () => { |
| 107 | + const repositoriesResponse = { data: { repositories } }; |
| 108 | + |
| 109 | + it(`should successfully check if the git remote is available and valid |
| 110 | + when the github remote URL is HTTPS based`, async () => { |
| 111 | + (existsSync as jest.Mock).mockReturnValueOnce(true); |
| 112 | + (getRemoteUrls as jest.Mock).mockResolvedValueOnce({ |
| 113 | + origin: 'https://github.com/test-user/eleventy-sample.git', |
| 114 | + }); |
| 115 | + const apolloClient = { |
| 116 | + query: jest.fn().mockResolvedValueOnce(repositoriesResponse), |
| 117 | + } as any; |
| 118 | + const githubAdapterInstance = new GitHub({ |
| 119 | + config: { projectBasePath: '/home/project1' }, |
| 120 | + apolloClient: apolloClient, |
| 121 | + } as any); |
| 122 | + |
| 123 | + const result = await githubAdapterInstance.checkGitRemoteAvailableAndValid(); |
| 124 | + |
| 125 | + expect(existsSync).toHaveBeenCalledWith('/home/project1/.git'); |
| 126 | + expect(getRemoteUrls as jest.Mock).toHaveBeenCalledWith('/home/project1/.git/config'); |
| 127 | + expect(apolloClient.query).toHaveBeenCalledWith({ query: repositoriesQuery }); |
| 128 | + expect(githubAdapterInstance.config.repository).toEqual({ |
| 129 | + __typename: 'GitRepository', |
| 130 | + id: '647250661', |
| 131 | + url: 'https://github.com/test-user/eleventy-sample', |
| 132 | + name: 'eleventy-sample', |
| 133 | + fullName: 'test-user/eleventy-sample', |
| 134 | + defaultBranch: 'main', |
| 135 | + }); |
| 136 | + expect(result).toBe(true); |
| 137 | + }); |
| 138 | + |
| 139 | + it(`should successfully check if the git remote is available and valid |
| 140 | + when the github remote URL is SSH based`, async () => { |
| 141 | + (existsSync as jest.Mock).mockReturnValueOnce(true); |
| 142 | + (getRemoteUrls as jest.Mock).mockResolvedValueOnce({ |
| 143 | + origin: 'git@github.com:test-user/eleventy-sample.git', |
| 144 | + }); |
| 145 | + const apolloClient = { |
| 146 | + query: jest.fn().mockResolvedValueOnce(repositoriesResponse), |
| 147 | + } as any; |
| 148 | + const githubAdapterInstance = new GitHub({ |
| 149 | + config: { projectBasePath: '/home/project1' }, |
| 150 | + apolloClient: apolloClient, |
| 151 | + } as any); |
| 152 | + |
| 153 | + const result = await githubAdapterInstance.checkGitRemoteAvailableAndValid(); |
| 154 | + |
| 155 | + expect(existsSync).toHaveBeenCalledWith('/home/project1/.git'); |
| 156 | + expect(getRemoteUrls as jest.Mock).toHaveBeenCalledWith('/home/project1/.git/config'); |
| 157 | + expect(apolloClient.query).toHaveBeenCalledWith({ query: repositoriesQuery }); |
| 158 | + expect(githubAdapterInstance.config.repository).toEqual({ |
| 159 | + __typename: 'GitRepository', |
| 160 | + id: '647250661', |
| 161 | + url: 'https://github.com/test-user/eleventy-sample', |
| 162 | + name: 'eleventy-sample', |
| 163 | + fullName: 'test-user/eleventy-sample', |
| 164 | + defaultBranch: 'main', |
| 165 | + }); |
| 166 | + expect(result).toBe(true); |
| 167 | + }); |
| 168 | + |
| 169 | + it('should log an error and exit if git config file does not exists', async () => { |
| 170 | + (existsSync as jest.Mock).mockReturnValueOnce(false); |
| 171 | + const githubAdapterInstance = new GitHub({ |
| 172 | + config: { projectBasePath: '/home/project1' }, |
| 173 | + log: logMock, |
| 174 | + exit: exitMock, |
| 175 | + } as any); |
| 176 | + let err; |
| 177 | + |
| 178 | + try { |
| 179 | + await githubAdapterInstance.checkGitRemoteAvailableAndValid(); |
| 180 | + } catch (error: any) { |
| 181 | + err = error; |
| 182 | + } |
| 183 | + |
| 184 | + expect(getRemoteUrls as jest.Mock).not.toHaveBeenCalled(); |
| 185 | + expect(logMock).toHaveBeenCalledWith('No Git repository configuration found at /home/project1.', 'error'); |
| 186 | + expect(logMock).toHaveBeenCalledWith( |
| 187 | + 'Please initialize a Git repository and try again, or use the File Upload option.', |
| 188 | + 'error', |
| 189 | + ); |
| 190 | + expect(exitMock).toHaveBeenCalledWith(1); |
| 191 | + expect(err).toEqual(new Error('1')); |
| 192 | + }); |
| 193 | + |
| 194 | + it(`should log an error if git repo remote url |
| 195 | + is unavailable and exit`, async () => { |
| 196 | + (existsSync as jest.Mock).mockReturnValueOnce(true); |
| 197 | + (getRemoteUrls as jest.Mock).mockResolvedValueOnce(undefined); |
| 198 | + const githubAdapterInstance = new GitHub({ |
| 199 | + config: { projectBasePath: '/home/project1' }, |
| 200 | + log: logMock, |
| 201 | + exit: exitMock, |
| 202 | + } as any); |
| 203 | + let err; |
| 204 | + |
| 205 | + try { |
| 206 | + await githubAdapterInstance.checkGitRemoteAvailableAndValid(); |
| 207 | + } catch (error: any) { |
| 208 | + err = error; |
| 209 | + } |
| 210 | + |
| 211 | + expect(existsSync).toHaveBeenCalledWith('/home/project1/.git'); |
| 212 | + expect(getRemoteUrls as jest.Mock).toHaveBeenCalledWith('/home/project1/.git/config'); |
| 213 | + expect(logMock).toHaveBeenCalledWith( |
| 214 | + `No Git remote origin URL found for the repository at /home/project1. |
| 215 | + Please add a git remote origin url and try again`, |
| 216 | + 'error', |
| 217 | + ); |
| 218 | + expect(exitMock).toHaveBeenCalledWith(1); |
| 219 | + expect(err).toEqual(new Error('1')); |
| 220 | + expect(githubAdapterInstance.config.repository).toBeUndefined(); |
| 221 | + }); |
| 222 | + |
| 223 | + it('should log an error and exit if GitHub app is uninstalled', async () => { |
| 224 | + (existsSync as jest.Mock).mockReturnValueOnce(true); |
| 225 | + (getRemoteUrls as jest.Mock).mockResolvedValueOnce({ |
| 226 | + origin: 'https://github.com/test-user/eleventy-sample.git', |
| 227 | + }); |
| 228 | + const apolloClient = { |
| 229 | + query: jest.fn().mockRejectedValue(new Error('GitHub app error')), |
| 230 | + } as any; |
| 231 | + const connectToAdapterOnUiMock = jest.spyOn(BaseClass.prototype, 'connectToAdapterOnUi').mockResolvedValueOnce(); |
| 232 | + const githubAdapterInstance = new GitHub({ |
| 233 | + config: { projectBasePath: '/home/project1' }, |
| 234 | + apolloClient: apolloClient, |
| 235 | + log: logMock, |
| 236 | + exit: exitMock, |
| 237 | + } as any); |
| 238 | + let err; |
| 239 | + |
| 240 | + try { |
| 241 | + await githubAdapterInstance.checkGitRemoteAvailableAndValid(); |
| 242 | + } catch (error: any) { |
| 243 | + err = error; |
| 244 | + } |
| 245 | + |
| 246 | + expect(existsSync).toHaveBeenCalledWith('/home/project1/.git'); |
| 247 | + expect(getRemoteUrls as jest.Mock).toHaveBeenCalledWith('/home/project1/.git/config'); |
| 248 | + expect(apolloClient.query).toHaveBeenCalled(); |
| 249 | + expect(connectToAdapterOnUiMock).toHaveBeenCalled(); |
| 250 | + expect(logMock).toHaveBeenCalledWith('GitHub app uninstalled. Please reconnect the app and try again', 'error'); |
| 251 | + expect(exitMock).toHaveBeenCalledWith(1); |
| 252 | + expect(err).toEqual(new Error('1')); |
| 253 | + }); |
| 254 | + |
| 255 | + it('should log an error and exit if repository is not found in the list of available repositories', async () => { |
| 256 | + (existsSync as jest.Mock).mockReturnValueOnce(true); |
| 257 | + (getRemoteUrls as jest.Mock).mockResolvedValueOnce({ |
| 258 | + origin: 'https://github.com/test-user/test-repo-2.git', |
| 259 | + }); |
| 260 | + const apolloClient = { |
| 261 | + query: jest.fn().mockResolvedValueOnce(repositoriesResponse), |
| 262 | + } as any; |
| 263 | + const githubAdapterInstance = new GitHub({ |
| 264 | + config: { projectBasePath: '/home/project1' }, |
| 265 | + log: logMock, |
| 266 | + exit: exitMock, |
| 267 | + apolloClient: apolloClient, |
| 268 | + } as any); |
| 269 | + let err; |
| 270 | + |
| 271 | + try { |
| 272 | + await githubAdapterInstance.checkGitRemoteAvailableAndValid(); |
| 273 | + } catch (error: any) { |
| 274 | + err = error; |
| 275 | + } |
| 276 | + |
| 277 | + expect(existsSync).toHaveBeenCalledWith('/home/project1/.git'); |
| 278 | + expect(getRemoteUrls as jest.Mock).toHaveBeenCalledWith('/home/project1/.git/config'); |
| 279 | + expect(apolloClient.query).toHaveBeenCalledWith({ query: repositoriesQuery }); |
| 280 | + expect(logMock).toHaveBeenCalledWith( |
| 281 | + 'Repository not added to the GitHub app. Please add it to the app’s repository access list and try again.', |
| 282 | + 'error', |
| 283 | + ); |
| 284 | + expect(exitMock).toHaveBeenCalledWith(1); |
| 285 | + expect(err).toEqual(new Error('1')); |
| 286 | + expect(githubAdapterInstance.config.repository).toBeUndefined(); |
| 287 | + }); |
| 288 | + }); |
| 289 | +}); |
0 commit comments