diff --git a/.env-cmdrc-template b/.env-cmdrc-template index de5fe45..b6b1849 100644 --- a/.env-cmdrc-template +++ b/.env-cmdrc-template @@ -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" } } \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 47ea93d..2de55b2 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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: diff --git a/src/external/gitops.js b/src/external/gitops.js new file mode 100644 index 0000000..81b22db --- /dev/null +++ b/src/external/gitops.js @@ -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); + } +} \ No newline at end of file diff --git a/src/routers/gitops.js b/src/routers/gitops.js index 6523376..95638a4 100644 --- a/src/routers/gitops.js +++ b/src/routers/gitops.js @@ -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; \ No newline at end of file diff --git a/src/services/gitops/index.js b/src/services/gitops/index.js index 9c743cb..68f81e5 100644 --- a/src/services/gitops/index.js +++ b/src/services/gitops/index.js @@ -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, @@ -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); } \ No newline at end of file diff --git a/tests/gitops-account.test.js b/tests/gitops-account.test.js index 5cdcfd6..560aff9 100644 --- a/tests/gitops-account.test.js +++ b/tests/gitops-account.test.js @@ -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'; @@ -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 () => {