Skip to content

Commit 279659e

Browse files
authored
GitOps Account - replaced update token with tokens API (#535)
1 parent 9884030 commit 279659e

File tree

9 files changed

+163
-141
lines changed

9 files changed

+163
-141
lines changed

npm-shrinkwrap.json

Lines changed: 78 additions & 79 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,16 @@
5050
"jsonwebtoken": "^9.0.2",
5151
"moment": "^2.30.1",
5252
"mongodb": "^6.9.0",
53-
"mongoose": "^8.7.1",
54-
"pino": "^9.4.0",
55-
"pino-pretty": "^11.2.2",
53+
"mongoose": "^8.7.2",
54+
"pino": "^9.5.0",
55+
"pino-pretty": "^11.3.0",
5656
"swagger-ui-express": "^5.0.1",
5757
"switcher-client": "^4.1.1",
5858
"validator": "^13.12.0"
5959
},
6060
"devDependencies": {
6161
"env-cmd": "^10.1.0",
62-
"eslint": "^9.12.0",
62+
"eslint": "^9.13.0",
6363
"jest": "^29.7.0",
6464
"jest-sonar-reporter": "^2.0.0",
6565
"node-notifier": "^10.0.1",

requests/Switcher API.postman_collection.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5657,7 +5657,7 @@
56575657
"header": [],
56585658
"body": {
56595659
"mode": "raw",
5660-
"raw": "{\r\n\t\"repository\": \"{{gitops_github_url}}\",\r\n\t\"token\": \"{{gitops_github_pat}}\",\r\n\t\"branch\": \"staging\",\r\n \"environment\": \"default\",\r\n\t\"domain\": {\r\n\t\t\"id\": \"{{gitops_domain_id}}\",\r\n\t\t\"name\": \"GitOps\"\r\n\t},\r\n\t\"settings\": {\r\n\t\t\"active\": true,\r\n\t\t\"window\": \"30s\",\r\n\t\t\"forceprune\": true\r\n\t}\t\r\n}",
5660+
"raw": "{\r\n\t\"repository\": \"{{gitops_github_url}}\",\r\n\t\"token\": \"{{gitops_github_pat}}\",\r\n\t\"branch\": \"develop\",\r\n \"path\": \"resources\",\r\n \"environment\": \"default\",\r\n\t\"domain\": {\r\n\t\t\"id\": \"{{gitops_domain_id}}\",\r\n\t\t\"name\": \"GitOps\"\r\n\t},\r\n\t\"settings\": {\r\n\t\t\"active\": true,\r\n\t\t\"window\": \"30s\",\r\n\t\t\"forceprune\": true\r\n\t}\t\r\n}",
56615661
"options": {
56625662
"raw": {
56635663
"language": "json"
@@ -5801,7 +5801,7 @@
58015801
"header": [],
58025802
"body": {
58035803
"mode": "raw",
5804-
"raw": "{\r\n\t\"repository\": \"{{gitops_github_url}}\",\r\n\t\"branch\": \"staging\",\r\n \"environment\": \"default\",\r\n\t\"domain\": {\r\n\t\t\"id\": \"{{gitops_domain_id}}\",\r\n\t\t\"name\": \"GitOps\"\r\n\t},\r\n\t\"settings\": {\r\n\t\t\"active\": true,\r\n\t\t\"window\": \"30s\",\r\n\t\t\"forceprune\": true\r\n\t}\t\r\n}",
5804+
"raw": "{\r\n \"environment\": \"default\",\r\n\t\"repository\": \"{{gitops_github_url}}\",\r\n\t\"branch\": \"develop\",\r\n \"token\": \"{{gitops_github_pat}}\",\r\n\t\"domain\": {\r\n\t\t\"id\": \"{{gitops_domain_id}}\",\r\n\t\t\"name\": \"GitOps\"\r\n\t},\r\n\t\"settings\": {\r\n\t\t\"active\": true,\r\n\t\t\"window\": \"3m\",\r\n\t\t\"forceprune\": true\r\n\t}\t\r\n}",
58055805
"options": {
58065806
"raw": {
58075807
"language": "json"
@@ -5823,7 +5823,7 @@
58235823
"response": []
58245824
},
58255825
{
5826-
"name": "GitOps Account - Update Token",
5826+
"name": "GitOps Account - Update Tokens",
58275827
"request": {
58285828
"auth": {
58295829
"type": "bearer",
@@ -5839,23 +5839,23 @@
58395839
"header": [],
58405840
"body": {
58415841
"mode": "raw",
5842-
"raw": "{\r\n \"environment\": \"default\",\r\n \"token\": \"{{gitops_github_pat}}\",\r\n\t\"domain\": {\r\n\t\t\"id\": \"{{gitops_domain_id}}\"\r\n\t}\r\n}",
5842+
"raw": "{\r\n \"token\": \"{{gitops_github_pat}}\",\r\n\t\"domain\": {\r\n\t\t\"id\": \"{{gitops_domain_id}}\"\r\n\t},\r\n \"environments\": [\r\n \"default\"\r\n ]\r\n}",
58435843
"options": {
58445844
"raw": {
58455845
"language": "json"
58465846
}
58475847
}
58485848
},
58495849
"url": {
5850-
"raw": "{{url}}/gitops/v1/account/token",
5850+
"raw": "{{url}}/gitops/v1/account/tokens",
58515851
"host": [
58525852
"{{url}}"
58535853
],
58545854
"path": [
58555855
"gitops",
58565856
"v1",
58575857
"account",
5858-
"token"
5858+
"tokens"
58595859
]
58605860
}
58615861
},

src/api-docs/paths/path-gitops.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export default {
9494
'/gitops/v1/account': {
9595
put: {
9696
tags: ['Switcher GitOps'],
97-
description: 'Update an account to receive gitops changes',
97+
description: 'Update an account for gitops repository access',
9898
requestBody: {
9999
content: commonSchemaContent('GitOpsAccountUpdateRequest')
100100
},
@@ -114,12 +114,12 @@ export default {
114114
}
115115
}
116116
},
117-
'/gitops/v1/account/token': {
117+
'/gitops/v1/account/tokens': {
118118
put: {
119119
tags: ['Switcher GitOps'],
120-
description: 'Update an account token to receive gitops changes',
120+
description: 'Update account tokens for gitops repository access',
121121
requestBody: {
122-
content: commonSchemaContent('GitOpsAccountTokenUpdateRequest')
122+
content: commonSchemaContent('GitOpsAccountTokensUpdateRequest')
123123
},
124124
responses: {
125125
200: {
@@ -140,7 +140,7 @@ export default {
140140
'/gitops/v1/account/forcesync': {
141141
put: {
142142
tags: ['Switcher GitOps'],
143-
description: 'Force sync an account to receive gitops changes',
143+
description: 'Force sync an account to lock up for gitops changes',
144144
requestBody: {
145145
content: commonSchemaContent('GitOpsAccountForceSyncRequest')
146146
},

src/api-docs/schemas/gitops.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,16 +177,19 @@ export default {
177177
type: 'object',
178178
properties: accountUpdateRequest
179179
},
180-
GitOpsAccountTokenUpdateRequest: {
180+
GitOpsAccountTokensUpdateRequest: {
181181
type: 'object',
182182
properties: {
183183
token: {
184184
type: 'string',
185185
description: 'The Git token'
186186
},
187-
environment: {
188-
type: 'string',
189-
description: 'The environment name'
187+
environments: {
188+
type: 'array',
189+
items: {
190+
type: 'string'
191+
},
192+
description: 'The environments to update'
190193
},
191194
domain: {
192195
type: 'object',

src/external/gitops.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,25 @@ export async function updateAccount(account) {
4646
}
4747
}
4848

49+
export async function updateAccountTokens(update, domainId) {
50+
try {
51+
const url = `${process.env.SWITCHER_GITOPS_URL}/account/tokens/${domainId}`;
52+
const response = await axios.put(url, update, {
53+
httpsAgent: agent(url),
54+
headers: headers(domainId)
55+
});
56+
57+
if (response.status !== 200) {
58+
throw new GitOpsError(`Failed to update accounts [${response.status}] ${JSON.stringify(response.data)}`,
59+
response.status);
60+
}
61+
62+
return response.data;
63+
} catch (e) {
64+
throw new GitOpsError(`Failed to update account: ${e.message}`, e.status);
65+
}
66+
}
67+
4968
export async function deleteAccount(domainId, environment) {
5069
try {
5170
const url = `${process.env.SWITCHER_GITOPS_URL}/account/${domainId}/${environment}`;

src/routers/gitops.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,18 @@ const windowValidation = (value) => {
1818
throw new Error('Invalid window value (minimum 30s)');
1919
}
2020

21+
if (parseInt(value) < 1) {
22+
throw new Error('Invalid window value (minimum 1[m/h])');
23+
}
24+
2125
return true;
2226
};
2327

2428
const accountValidators = [
2529
body('token').isString().optional(),
2630
body('repository').isURL().withMessage('Invalid repository URL'),
2731
body('branch').isString().withMessage('Invalid branch name'),
32+
body('path').isString().optional().withMessage('Invalid path'),
2833
body('environment').isString().withMessage('Invalid environment name'),
2934
body('domain.id').isMongoId().withMessage('Invalid domain ID'),
3035
body('domain.name').isString().withMessage('Invalid domain name'),
@@ -74,10 +79,8 @@ router.post('/gitops/v1/account/unsubscribe', auth, [
7479
}
7580
});
7681

77-
router.put('/gitops/v1/account', auth, [
78-
body('environment').isString(),
79-
body('domain.id').isMongoId().withMessage('Invalid domain ID'),
80-
], validate, featureFlag, async (req, res) => {
82+
router.put('/gitops/v1/account', auth, accountValidators, validate,
83+
featureFlag, async (req, res) => {
8184
try {
8285
const account = await Service.updateAccount(req.body);
8386
res.status(200).send(account);
@@ -86,13 +89,13 @@ router.put('/gitops/v1/account', auth, [
8689
}
8790
});
8891

89-
router.put('/gitops/v1/account/token', auth, [
90-
body('environment').isString(),
92+
router.put('/gitops/v1/account/tokens', auth, [
9193
body('token').isString(),
94+
body('environments').isArray(),
9295
body('domain.id').isMongoId().withMessage('Invalid domain ID'),
9396
], validate, featureFlag, async (req, res) => {
9497
try {
95-
const account = await Service.updateAccountToken(req.body);
98+
const account = await Service.updateAccountTokens(req.body);
9699
res.status(200).send(account);
97100
} catch (e) {
98101
responseExceptionSilent(res, e, 500, 'Account token update failed');

src/services/gitops/index.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,11 @@ export async function updateAccount(account) {
3737
return GitOpsFacade.updateAccount(account);
3838
}
3939

40-
export async function updateAccountToken(account) {
41-
return GitOpsFacade.updateAccount({
42-
environment: account.environment,
40+
export async function updateAccountTokens(account) {
41+
return GitOpsFacade.updateAccountTokens({
4342
token: account.token,
44-
domain: {
45-
id: account.domain.id
46-
}
47-
});
43+
environments: account.environments
44+
}, account.domain.id);
4845
}
4946

5047
export async function forceSyncAccount(account) {

tests/gitops-account.test.js

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,19 @@ describe('GitOps Account - Subscribe', () => {
183183
expect(req.body.errors[0].msg).toBe('Invalid window value (minimum 30s)');
184184
});
185185

186+
test('GITOPS_ACCOUNT_SUITE - Should return error - window cannot be lower than 1(m/h)', async () => {
187+
const payload = JSON.parse(JSON.stringify(VALID_SUBSCRIPTION_REQUEST));
188+
payload.settings.window = "0m";
189+
190+
const req = await request(app)
191+
.post('/gitops/v1/account/subscribe')
192+
.set('Authorization', `Bearer ${adminMasterAccountToken}`)
193+
.send(payload)
194+
.expect(422);
195+
196+
expect(req.body.errors[0].msg).toBe('Invalid window value (minimum 1[m/h])');
197+
});
198+
186199
test('GITOPS_ACCOUNT_SUITE - Should return error - window cannot use different units than [s,m,h]', async () => {
187200
const payload = JSON.parse(JSON.stringify(VALID_SUBSCRIPTION_REQUEST));
188201
payload.settings.window = '1d';
@@ -204,6 +217,7 @@ describe('GitOps Account - Update', () => {
204217
repository: 'https://github.com/switcherapi/switcher-gitops-fixture',
205218
branch: 'main',
206219
environment: EnvType.DEFAULT,
220+
path: 'path/to/file',
207221
domain: {
208222
id: String(domainId),
209223
name: 'Test Domain'
@@ -294,55 +308,42 @@ describe('GitOps Account - Update', () => {
294308

295309
})
296310

297-
describe('GitOps Account - Update Token', () => {
311+
describe('GitOps Account - Update All Token', () => {
298312
beforeAll(setupDatabase);
299313

300-
const VALID_UPDATE_REQUEST = {
301-
repository: 'https://github.com/switcherapi/switcher-gitops-fixture',
302-
branch: 'main',
303-
environment: EnvType.DEFAULT,
304-
domain: {
305-
id: String(domainId),
306-
name: 'Test Domain'
307-
},
308-
settings: {
309-
active: true,
310-
window: '30s',
311-
forceprune: true
312-
}
313-
};
314-
315314
const VALID_TOKEN_UPDATE_REQUEST = {
316-
environment: EnvType.DEFAULT,
315+
environments: [EnvType.DEFAULT],
317316
token: '123456',
318317
domain: {
319318
id: String(domainId)
320319
}
321320
};
322321

323-
test('GITOPS_ACCOUNT_SUITE - Should update account token', async () => {
324-
// given
325-
const expectedResponse = JSON.parse(JSON.stringify(VALID_UPDATE_REQUEST));
326-
expectedResponse.token = '...123';
327-
322+
test('GITOPS_ACCOUNT_SUITE - Should update account tokens', async () => {
328323
const postStub = sinon.stub(axios, 'put').resolves({
329324
status: 200,
330-
data: expectedResponse
325+
data: {
326+
result: true,
327+
message: 'Account tokens updated successfully'
328+
}
331329
});
332330

333331
// test
334332
const req = await request(app)
335-
.put('/gitops/v1/account/token')
333+
.put('/gitops/v1/account/tokens')
336334
.set('Authorization', `Bearer ${adminMasterAccountToken}`)
337335
.send(VALID_TOKEN_UPDATE_REQUEST)
338336
.expect(200);
339337

340338
// assert
341-
expect(req.body).toMatchObject(expectedResponse);
339+
expect(req.body).toMatchObject({
340+
result: true,
341+
message: 'Account tokens updated successfully'
342+
});
342343
postStub.restore();
343344
});
344345

345-
test('GITOPS_ACCOUNT_SUITE - Should return error - error updating account token', async () => {
346+
test('GITOPS_ACCOUNT_SUITE - Should return error - error updating account tokens', async () => {
346347
// given
347348
const postStub = sinon.stub(axios, 'put').resolves({
348349
status: 500,
@@ -353,7 +354,7 @@ describe('GitOps Account - Update Token', () => {
353354

354355
// test
355356
const req = await request(app)
356-
.put('/gitops/v1/account/token')
357+
.put('/gitops/v1/account/tokens')
357358
.set('Authorization', `Bearer ${adminMasterAccountToken}`)
358359
.send(VALID_TOKEN_UPDATE_REQUEST)
359360
.expect(500);
@@ -374,7 +375,7 @@ describe('GitOps Account - Update Token', () => {
374375

375376
// test
376377
const req = await request(app)
377-
.put('/gitops/v1/account/token')
378+
.put('/gitops/v1/account/tokens')
378379
.set('Authorization', `Bearer ${adminMasterAccountToken}`)
379380
.send(VALID_TOKEN_UPDATE_REQUEST)
380381
.expect(500);
@@ -389,7 +390,7 @@ describe('GitOps Account - Update Token', () => {
389390
delete payload.domain.id;
390391

391392
const req = await request(app)
392-
.put('/gitops/v1/account/token')
393+
.put('/gitops/v1/account/tokens')
393394
.set('Authorization', `Bearer ${adminMasterAccountToken}`)
394395
.send(payload)
395396
.expect(422);

0 commit comments

Comments
 (0)