|
| 1 | +import { http, HttpResponse } from 'msw' |
| 2 | +import { setupServer } from 'msw/node' |
| 3 | +import { Client, ClientConfiguration } from '@octopusdeploy/api-client' |
| 4 | +import { listEnvironments } from '../../src/api-wrapper' |
| 5 | + |
| 6 | +describe('listEnvironments', () => { |
| 7 | + const testData = { |
| 8 | + server: 'https://my.octopus.app', |
| 9 | + spaceName: 'Default', |
| 10 | + apiKey: 'API-XXXXXXXXXXXXXXXXXXXXXXXX', |
| 11 | + environmentId1: 'Environments-123', |
| 12 | + environmentId2: 'Environments-124', |
| 13 | + environmentName1: 'Production', |
| 14 | + environmentName2: 'Staging' |
| 15 | + } |
| 16 | + |
| 17 | + const config: ClientConfiguration = { |
| 18 | + userAgentApp: 'GitHubActions deprovision-ephemeral-environment', |
| 19 | + instanceURL: testData.server, |
| 20 | + apiKey: testData.apiKey |
| 21 | + } |
| 22 | + |
| 23 | + // When running a version of Octopus Server without EE's enabled |
| 24 | + describe('when v2 endpoint is not available', () => { |
| 25 | + test('should successfully retrieve environments using v1 endpoint', async () => { |
| 26 | + const envIds = [testData.environmentId1, testData.environmentId2] |
| 27 | + |
| 28 | + const server = setupServer( |
| 29 | + http.get('https://my.octopus.app/api/:spaceId/environments/v2', () => { |
| 30 | + return HttpResponse.json({ message: 'Not Found' }, { status: 404 }) |
| 31 | + }), |
| 32 | + http.get('https://my.octopus.app/api/:spaceId/environments', () => { |
| 33 | + return HttpResponse.json({ |
| 34 | + Items: [ |
| 35 | + { |
| 36 | + Id: testData.environmentId1, |
| 37 | + Name: testData.environmentName1 |
| 38 | + }, |
| 39 | + { |
| 40 | + Id: testData.environmentId2, |
| 41 | + Name: testData.environmentName2 |
| 42 | + } |
| 43 | + ] |
| 44 | + }) |
| 45 | + }), |
| 46 | + http.get('https://my.octopus.app/api', () => { |
| 47 | + return HttpResponse.json([{}]) |
| 48 | + }), |
| 49 | + http.get('https://my.octopus.app/api/spaces', () => { |
| 50 | + return HttpResponse.json({ |
| 51 | + Items: [ |
| 52 | + { |
| 53 | + Name: 'Default', |
| 54 | + Id: 'Spaces-1' |
| 55 | + } |
| 56 | + ] |
| 57 | + }) |
| 58 | + }) |
| 59 | + ) |
| 60 | + server.listen() |
| 61 | + |
| 62 | + const client = await Client.create(config) |
| 63 | + |
| 64 | + const environments = await listEnvironments(client, envIds, testData.spaceName) |
| 65 | + |
| 66 | + expect(environments.Items).toHaveLength(2) |
| 67 | + expect(environments.Items[0].Name).toBe(testData.environmentName1) |
| 68 | + expect(environments.Items[1].Name).toBe(testData.environmentName2) |
| 69 | + |
| 70 | + server.close() |
| 71 | + }) |
| 72 | + }) |
| 73 | + |
| 74 | + // When running a version of Octopus Server with EE's enabled before 2025.4 |
| 75 | + describe('when v2 endpoint cannot find environments', () => { |
| 76 | + test('should successfully retrieve environments using v1 endpoint', async () => { |
| 77 | + const envIds = [testData.environmentId1, testData.environmentId2] |
| 78 | + |
| 79 | + const server = setupServer( |
| 80 | + http.get('https://my.octopus.app/api/:spaceId/environments/v2', () => { |
| 81 | + return HttpResponse.json({ Items: [] }) |
| 82 | + }), |
| 83 | + http.get('https://my.octopus.app/api/:spaceId/environments', () => { |
| 84 | + return HttpResponse.json({ |
| 85 | + Items: [ |
| 86 | + { |
| 87 | + Id: testData.environmentId1, |
| 88 | + Name: testData.environmentName1 |
| 89 | + }, |
| 90 | + { |
| 91 | + Id: testData.environmentId2, |
| 92 | + Name: testData.environmentName2 |
| 93 | + } |
| 94 | + ] |
| 95 | + }) |
| 96 | + }), |
| 97 | + http.get('https://my.octopus.app/api', () => { |
| 98 | + return HttpResponse.json([{}]) |
| 99 | + }), |
| 100 | + http.get('https://my.octopus.app/api/spaces', () => { |
| 101 | + return HttpResponse.json({ |
| 102 | + Items: [ |
| 103 | + { |
| 104 | + Name: 'Default', |
| 105 | + Id: 'Spaces-1' |
| 106 | + } |
| 107 | + ] |
| 108 | + }) |
| 109 | + }) |
| 110 | + ) |
| 111 | + server.listen() |
| 112 | + |
| 113 | + const client = await Client.create(config) |
| 114 | + |
| 115 | + const environments = await listEnvironments(client, envIds, testData.spaceName) |
| 116 | + |
| 117 | + expect(environments.Items).toHaveLength(2) |
| 118 | + expect(environments.Items[0].Name).toBe(testData.environmentName1) |
| 119 | + expect(environments.Items[1].Name).toBe(testData.environmentName2) |
| 120 | + |
| 121 | + server.close() |
| 122 | + }) |
| 123 | + }) |
| 124 | +}) |
0 commit comments