|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @flow strict-local |
| 8 | + * @format |
| 9 | + */ |
| 10 | + |
| 11 | +'use strict'; |
| 12 | + |
| 13 | +const { |
| 14 | + cleanupMavenStagingRepositories, |
| 15 | + createAuthorizationHeader, |
| 16 | +} = require('../cleanup-maven-staging-repositories'); |
| 17 | + |
| 18 | +const fetchMock = jest.fn(); |
| 19 | + |
| 20 | +// $FlowFixMe[cannot-write] |
| 21 | +global.fetch = fetchMock; |
| 22 | + |
| 23 | +const API_URL = 'https://sonatype.example/'; |
| 24 | +const PORTAL_API_URL = 'https://portal.example/'; |
| 25 | +const CREDENTIALS = {username: 'token-user', password: 'token-password'}; |
| 26 | +const DESCRIPTION = 'react-native:v0.86.2:github-run-1234'; |
| 27 | + |
| 28 | +describe('cleanupMavenStagingRepositories', () => { |
| 29 | + beforeEach(() => { |
| 30 | + jest.spyOn(console, 'log').mockImplementation(() => {}); |
| 31 | + jest.resetAllMocks(); |
| 32 | + }); |
| 33 | + |
| 34 | + test('drops matching repositories in every state', async () => { |
| 35 | + fetchMock.mockResolvedValueOnce({ |
| 36 | + ok: true, |
| 37 | + json: async () => ({ |
| 38 | + repositories: [ |
| 39 | + {key: 'matching-1', state: 'open', description: DESCRIPTION}, |
| 40 | + {key: 'other-run', state: 'open', description: 'another run'}, |
| 41 | + {key: 'matching-2', state: 'open', description: DESCRIPTION}, |
| 42 | + {key: 'already-closed', state: 'closed', description: DESCRIPTION}, |
| 43 | + { |
| 44 | + key: 'already-released', |
| 45 | + state: 'released', |
| 46 | + description: DESCRIPTION, |
| 47 | + }, |
| 48 | + ], |
| 49 | + }), |
| 50 | + }); |
| 51 | + fetchMock.mockResolvedValue({ok: true}); |
| 52 | + |
| 53 | + await expect( |
| 54 | + cleanupMavenStagingRepositories(DESCRIPTION, CREDENTIALS, API_URL), |
| 55 | + ).resolves.toBe(4); |
| 56 | + |
| 57 | + expect(fetchMock).toHaveBeenCalledTimes(5); |
| 58 | + expect( |
| 59 | + fetchMock.mock.calls.map(([url, options]) => [ |
| 60 | + url.toString(), |
| 61 | + options.method, |
| 62 | + ]), |
| 63 | + ).toEqual([ |
| 64 | + ['https://sonatype.example/manual/search/repositories?ip=any', 'GET'], |
| 65 | + ['https://sonatype.example/manual/drop/repository/matching-1', 'DELETE'], |
| 66 | + ['https://sonatype.example/manual/drop/repository/matching-2', 'DELETE'], |
| 67 | + [ |
| 68 | + 'https://sonatype.example/manual/drop/repository/already-closed', |
| 69 | + 'DELETE', |
| 70 | + ], |
| 71 | + [ |
| 72 | + 'https://sonatype.example/manual/drop/repository/already-released', |
| 73 | + 'DELETE', |
| 74 | + ], |
| 75 | + ]); |
| 76 | + expect(fetchMock.mock.calls[0][1].headers).toEqual({ |
| 77 | + Authorization: createAuthorizationHeader(CREDENTIALS), |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + test('does nothing when no repository matches', async () => { |
| 82 | + fetchMock.mockResolvedValueOnce({ |
| 83 | + ok: true, |
| 84 | + json: async () => ({repositories: []}), |
| 85 | + }); |
| 86 | + |
| 87 | + await expect( |
| 88 | + cleanupMavenStagingRepositories(DESCRIPTION, CREDENTIALS, API_URL), |
| 89 | + ).resolves.toBe(0); |
| 90 | + |
| 91 | + expect(fetchMock).toHaveBeenCalledTimes(1); |
| 92 | + }); |
| 93 | + |
| 94 | + test('waits for validation and drops the associated Portal deployment', async () => { |
| 95 | + fetchMock.mockResolvedValueOnce({ |
| 96 | + ok: true, |
| 97 | + json: async () => ({ |
| 98 | + repositories: [ |
| 99 | + { |
| 100 | + key: 'closed-repository', |
| 101 | + state: 'closed', |
| 102 | + description: DESCRIPTION, |
| 103 | + portal_deployment_id: 'deployment-1', |
| 104 | + }, |
| 105 | + ], |
| 106 | + }), |
| 107 | + }); |
| 108 | + fetchMock.mockResolvedValueOnce({ |
| 109 | + ok: true, |
| 110 | + json: async () => ({deploymentState: 'VALIDATING'}), |
| 111 | + }); |
| 112 | + fetchMock.mockResolvedValueOnce({ |
| 113 | + ok: true, |
| 114 | + json: async () => ({deploymentState: 'VALIDATED'}), |
| 115 | + }); |
| 116 | + fetchMock.mockResolvedValue({ok: true}); |
| 117 | + |
| 118 | + await expect( |
| 119 | + cleanupMavenStagingRepositories( |
| 120 | + DESCRIPTION, |
| 121 | + CREDENTIALS, |
| 122 | + API_URL, |
| 123 | + PORTAL_API_URL, |
| 124 | + 0, |
| 125 | + 2, |
| 126 | + ), |
| 127 | + ).resolves.toBe(1); |
| 128 | + |
| 129 | + expect( |
| 130 | + fetchMock.mock.calls.map(([url, options]) => [ |
| 131 | + url.toString(), |
| 132 | + options.method, |
| 133 | + ]), |
| 134 | + ).toEqual([ |
| 135 | + ['https://sonatype.example/manual/search/repositories?ip=any', 'GET'], |
| 136 | + [ |
| 137 | + 'https://portal.example/api/v1/publisher/status?id=deployment-1', |
| 138 | + 'POST', |
| 139 | + ], |
| 140 | + [ |
| 141 | + 'https://portal.example/api/v1/publisher/status?id=deployment-1', |
| 142 | + 'POST', |
| 143 | + ], |
| 144 | + [ |
| 145 | + 'https://portal.example/api/v1/publisher/deployment/deployment-1', |
| 146 | + 'DELETE', |
| 147 | + ], |
| 148 | + [ |
| 149 | + 'https://sonatype.example/manual/drop/repository/closed-repository', |
| 150 | + 'DELETE', |
| 151 | + ], |
| 152 | + ]); |
| 153 | + }); |
| 154 | + |
| 155 | + test('keeps an already published Portal deployment', async () => { |
| 156 | + fetchMock.mockResolvedValueOnce({ |
| 157 | + ok: true, |
| 158 | + json: async () => ({ |
| 159 | + repositories: [ |
| 160 | + { |
| 161 | + key: 'released-repository', |
| 162 | + state: 'released', |
| 163 | + description: DESCRIPTION, |
| 164 | + portal_deployment_id: 'deployment-2', |
| 165 | + }, |
| 166 | + ], |
| 167 | + }), |
| 168 | + }); |
| 169 | + fetchMock.mockResolvedValueOnce({ |
| 170 | + ok: true, |
| 171 | + json: async () => ({deploymentState: 'PUBLISHED'}), |
| 172 | + }); |
| 173 | + fetchMock.mockResolvedValueOnce({ok: true}); |
| 174 | + |
| 175 | + await expect( |
| 176 | + cleanupMavenStagingRepositories( |
| 177 | + DESCRIPTION, |
| 178 | + CREDENTIALS, |
| 179 | + API_URL, |
| 180 | + PORTAL_API_URL, |
| 181 | + ), |
| 182 | + ).resolves.toBe(1); |
| 183 | + |
| 184 | + expect( |
| 185 | + fetchMock.mock.calls.map(([url, options]) => [ |
| 186 | + url.toString(), |
| 187 | + options.method, |
| 188 | + ]), |
| 189 | + ).toEqual([ |
| 190 | + ['https://sonatype.example/manual/search/repositories?ip=any', 'GET'], |
| 191 | + [ |
| 192 | + 'https://portal.example/api/v1/publisher/status?id=deployment-2', |
| 193 | + 'POST', |
| 194 | + ], |
| 195 | + [ |
| 196 | + 'https://sonatype.example/manual/drop/repository/released-repository', |
| 197 | + 'DELETE', |
| 198 | + ], |
| 199 | + ]); |
| 200 | + }); |
| 201 | + |
| 202 | + test('fails when Sonatype returns an error', async () => { |
| 203 | + fetchMock.mockResolvedValueOnce({ |
| 204 | + ok: false, |
| 205 | + status: 503, |
| 206 | + text: async () => 'temporarily unavailable', |
| 207 | + }); |
| 208 | + |
| 209 | + await expect( |
| 210 | + cleanupMavenStagingRepositories(DESCRIPTION, CREDENTIALS, API_URL), |
| 211 | + ).rejects.toThrow( |
| 212 | + 'Sonatype request to /manual/search/repositories failed with HTTP 503: temporarily unavailable', |
| 213 | + ); |
| 214 | + }); |
| 215 | + |
| 216 | + test('rejects an invalid search response', async () => { |
| 217 | + fetchMock.mockResolvedValueOnce({ |
| 218 | + ok: true, |
| 219 | + json: async () => ({}), |
| 220 | + }); |
| 221 | + |
| 222 | + await expect( |
| 223 | + cleanupMavenStagingRepositories(DESCRIPTION, CREDENTIALS, API_URL), |
| 224 | + ).rejects.toThrow( |
| 225 | + 'Sonatype returned an invalid repository search response.', |
| 226 | + ); |
| 227 | + }); |
| 228 | +}); |
0 commit comments