Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .env-cmdrc-template
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"SWITCHER_API_ENVIRONMENT": "default",

"SWITCHER_SLACK_JWT_SECRET": "MOCK_SWITCHER_SLACK_JWT_SECRET",
"SWITCHER_GITOPS_JWT_SECRET": "MOCK_SWITCHER_GITOPS_JWT_SECRET"
"SWITCHER_GITOPS_JWT_SECRET": "MOCK_SWITCHER_GITOPS_JWT_SECRET",
"SWITCHER_GITOPS_URL": "http://localhost:8000"
}
}
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ services:

- SWITCHER_SLACK_JWT_SECRET=${SWITCHER_SLACK_JWT_SECRET}
- SWITCHER_GITOPS_JWT_SECRET=${SWITCHER_GITOPS_JWT_SECRET}
- SWITCHER_GITOPS_URL=${SWITCHER_GITOPS_URL}
depends_on:
- mongodb
volumes:
Expand Down
20 changes: 20 additions & 0 deletions src/external/gitops.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import axios from 'axios';

export async function createAccount(account) {
const url = `${process.env.SWITCHER_GITOPS_URL}/account`;
const response = await axios.post(url, account);

if (response.status !== 201) {
throw new GitOpsError(`Failed to create account [${response.status}] ${JSON.stringify(response.data)}`);
}

return response.data;
}

export class GitOpsError extends Error {
constructor(message) {
super(message);
this.name = this.constructor.name;
Error.captureStackTrace(this, this.constructor);
}
}
7 changes: 6 additions & 1 deletion src/routers/gitops.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ router.post('/gitops/v1/push', gitopsAuth, featureFlag, [

router.post('/gitops/v1/account/subscribe', auth, accountValidators, validate,
featureFlag, async (req, res) => {
res.status(201).send();
try {
const account = await Service.subscribeAccount(req.body);
res.status(201).send(account);
} catch (e) {
responseExceptionSilent(res, e, 500, 'Account subscription failed');
}
});

export default router;
5 changes: 5 additions & 0 deletions src/services/gitops/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getDomainById, updateDomainVersion } from '../domain.js';
import { processChanged } from './push-changed.js';
import { processDeleted } from './push-deleted.js';
import { processNew } from './push-new.js';
import * as GitOpsFacade from '../../external/gitops.js';

const CHANGE_PROCESSES = Object.freeze({
NEW: processNew,
Expand All @@ -26,4 +27,8 @@ export async function pushChanges(domainId, environment, changes) {
message: 'Changes applied successfully',
version: domain.lastUpdate
};
}

export async function subscribeAccount(account) {
return GitOpsFacade.createAccount(account);
}
39 changes: 38 additions & 1 deletion tests/gitops-account.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import mongoose from 'mongoose';
import axios from 'axios';
import sinon from 'sinon';
import request from 'supertest';
import { Client } from 'switcher-client';
import app from '../src/app';
Expand Down Expand Up @@ -56,11 +58,46 @@ describe('GitOps Account - Subscribe', () => {
beforeAll(setupDatabase);

test('GITOPS_ACCOUNT_SUITE - Should subscribe account', async () => {
await request(app)
// given
const expectedResponse = JSON.parse(JSON.stringify(VALID_SUBSCRIPTION_REQUEST));
expectedResponse.token = '...123';

const postStub = sinon.stub(axios, 'post').resolves({
status: 201,
data: expectedResponse
});

// test
const req = await request(app)
.post('/gitops/v1/account/subscribe')
.set('Authorization', `Bearer ${adminMasterAccountToken}`)
.send(VALID_SUBSCRIPTION_REQUEST)
.expect(201);

// assert
expect(req.body).toMatchObject(expectedResponse);
postStub.restore();
});

test('GITOPS_ACCOUNT_SUITE - Should return error - error creating account', async () => {
// given
const postStub = sinon.stub(axios, 'post').resolves({
status: 500,
data: {
error: 'Error creating account'
}
});

// test
const req = await request(app)
.post('/gitops/v1/account/subscribe')
.set('Authorization', `Bearer ${adminMasterAccountToken}`)
.send(VALID_SUBSCRIPTION_REQUEST)
.expect(500);

// assert
expect(req.body.error).toBe('Account subscription failed');
postStub.restore();
});

test('GITOPS_ACCOUNT_SUITE - Should return error - missing domain.id', async () => {
Expand Down