Skip to content

Commit

Permalink
review PR
Browse files Browse the repository at this point in the history
  • Loading branch information
yoandl committed Sep 2, 2022
1 parent e9151a0 commit e7351d3
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 42 deletions.
4 changes: 2 additions & 2 deletions common/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const {
sampleView: releaseTagSelection,
} = require('../../run/services/slack/surfaces/modals/deploy-release/release-tag-selection');
const {
sampleView: CreateAppOnScalingoSelection,
sampleView: createAppOnScalingoSelection,
} = require('../../run/services/slack/surfaces/modals/scalingo-apps/application-creation');
const {
sampleView: releaseDeploymentConfirmation,
Expand All @@ -31,7 +31,7 @@ module.exports = {
const views = [
{ name: 'release-type-selection', view: releaseTypeSelection() },
{ name: 'release-tag-selection', view: releaseTagSelection() },
{ name: 'create-app-on-scalingo', view: CreateAppOnScalingoSelection() },
{ name: 'create-app-on-scalingo', view: createAppOnScalingoSelection() },
{ name: 'release-deployment-confirmation', view: releaseDeploymentConfirmation() },
{ name: 'release-publication-confirmation', view: releasePublicationConfirmation() },
{ name: 'application-creation-confirmation', view: submitApplicationNameSelection() },
Expand Down
14 changes: 7 additions & 7 deletions common/models/ScalingoAppName.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
//TODO use EnvVars
const config = require('../../config');
const regexp = /^([a-zA-Z0-9]+-)+[a-zA-Z0-9]+$/;

const alphanumericAndDashOnly = /^([a-zA-Z0-9]+-)+[a-zA-Z0-9]+$/;
const prefix = 'pix-';
class ScalingoAppName {
static isValidAppName(applicationName) {
const scalingoAppNameSuffix = config.scalingo.validAppSuffix;
const appNameMatchesRegex = applicationName.search(regexp) >= 0;
static isApplicationNameValid(applicationName) {
const suffix = config.scalingo.validAppSuffix;
const appNameMatchesRegex = applicationName.search(alphanumericAndDashOnly) >= 0;
const appNameHasCorrectLength = applicationName.length >= 6 && applicationName.length <= 46;
const appNameStartsWithPix = applicationName.startsWith('pix-');
const appNameEndsWithCorrectSuffix = scalingoAppNameSuffix.includes(applicationName.split('-').slice(-1)[0]);
const appNameStartsWithPix = applicationName.startsWith(prefix);
const appNameEndsWithCorrectSuffix = suffix.includes(applicationName.split('-').slice(-1)[0]);
return appNameMatchesRegex && appNameHasCorrectLength && appNameStartsWithPix && appNameEndsWithCorrectSuffix;
}
}
Expand Down
9 changes: 4 additions & 5 deletions common/services/scalingo-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class ScalingoClient {
}

static async getInstance(environment) {
console.log(environment);
const { token, apiUrl } = config.scalingo[environment];
console.log(token, apiUrl);
const client = await scalingo.clientFromToken(token, { apiUrl });
Expand Down Expand Up @@ -85,14 +84,14 @@ class ScalingoClient {
throw err;
}
}
async inviteCollaborator(appId, email) {
const { invitation_link } = await this.client.Collaborators.invite(appId, email);
async inviteCollaborator(applicationId, collaboratorEmail) {
const { invitation_link } = await this.client.Collaborators.invite(applicationId, collaboratorEmail);
return invitation_link;
}

async createApp(appName) {
async createApplication(name) {
const app = {
name: appName,
name: name,
};
const { id } = await this.client.Apps.create(app);
return id;
Expand Down
5 changes: 3 additions & 2 deletions run/services/slack/shortcuts.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const axios = require('axios');
const deployReleaseTagSelectionModal = require('./surfaces/modals/deploy-release/release-tag-selection');
const createAppOnScalingoModal = require('./surfaces/modals/scalingo-apps/application-creation');
const config = require('../../../config');
const openViewUrl = 'https://slack.com/api/views.open';

module.exports = {
openViewDeployReleaseTagSelectionCallbackId: deployReleaseTagSelectionModal.callbackId,
Expand All @@ -11,7 +12,7 @@ module.exports = {
openViewDeployReleaseTagSelection(payload) {
const options = {
method: 'POST',
url: 'https://slack.com/api/views.open',
url: openViewUrl,
headers: {
'content-type': 'application/json',
authorization: `Bearer ${config.slack.botToken}`,
Expand All @@ -24,7 +25,7 @@ module.exports = {
openViewCreateAppOnScalingoSelection(payload) {
const options = {
method: 'POST',
url: 'https://slack.com/api/views.open',
url: openViewUrl,
headers: {
'content-type': 'application/json',
authorization: `Bearer ${config.slack.botToken}`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function modal(applicationName, applicationEnvironment, userEmail) {
close: 'Annuler',
}).blocks([
Blocks.Section({
text: `Vous vous apprêtez à créer l'application *${applicationName}* en *${applicationEnvironment}* avec ${userEmail} en collab. `,
text: `Vous vous apprêtez à créer l'application *${applicationName}* en *${applicationEnvironment}* et à inviter cet adesse email en tant que collaborateur : *${userEmail}*`,
}),
]);
}
Expand All @@ -28,7 +28,7 @@ module.exports = (applicationName, applicationEnvironment, userEmail) => {
};

module.exports.sampleView = () => {
return modal('pix-application-name-recette', 'recette', 'pixou@pix.fr');
return modal('pix-application-name-recette', 'recette', 'john.doe@pix.fr');
};

module.exports.callbackId = callbackId;
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ function modal() {
initialValue: 'pix-super-application-recette',
})
),
Blocks.Input({ blockId: 'application-env', label: 'Quelle region ?' }).element(
Elements.StaticSelect({ placeholder: 'Choisis ta région' })
Blocks.Input({ blockId: 'application-env', label: 'Quelle région ?' }).element(
Elements.StaticSelect({ placeholder: 'Choisis la région' })
.actionId('item')
.options(regions.map((item) => Bits.Option({ text: item.name, value: item.id })))
),
Expand Down
5 changes: 2 additions & 3 deletions run/services/slack/view-submissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports = {
const applicationEnvironment =
payload.view.state.values['application-env']['item']['selected_option']['text']['text'];
const userEmail = await slackGetUserInfos.getUserEmail(payload.user.id);
if (!ScalingoAppName.isValidAppName(applicationName)) {
if (!ScalingoAppName.isApplicationNameValid(applicationName)) {
return `${applicationName} is incorrect`;
}
return openModalApplicationCreationConfirmation(applicationName, applicationEnvironment, userEmail);
Expand All @@ -44,8 +44,7 @@ module.exports = {
async submitCreateAppOnScalingoConfirmation(payload) {
const { applicationName, applicationEnvironment, userEmail } = JSON.parse(payload.view.private_metadata);
const client = await ScalingoClient.getInstance(applicationEnvironment);
//TODO check if user email exist
const appId = await client.createApp(applicationName);
const appId = await client.createAplication(applicationName);
const invitationLink = await client.inviteCollaborator(appId, userEmail);
const message = `app ${applicationName} created <${invitationLink}|invitation link>`;
slackPostMessageService.postMessage(message);
Expand Down
12 changes: 7 additions & 5 deletions test/acceptance/run/slack_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,12 @@ describe('Acceptance | Run | Slack', function () {
block_id: 'application-env',
label: {
type: 'plain_text',
text: 'Quelle region ?',
text: 'Quelle région ?',
},
element: {
placeholder: {
type: 'plain_text',
text: 'Choisis ta région',
text: 'Choisis la région',
},
action_id: 'item',
options: [
Expand Down Expand Up @@ -313,12 +313,14 @@ describe('Acceptance | Run | Slack', function () {
callback_id: 'scalingo-app-creation',
trigger_id: 'payload id',
};
// when
const res = await server.inject({
method: 'POST',
url: '/run/slack/interactive-endpoint',
headers: createSlackWebhookSignatureHeaders(JSON.stringify(body)),
payload: body,
});
// then
expect(res.statusCode).to.equal(204);
expect(slackCall.isDone()).to.be.true;
});
Expand All @@ -329,7 +331,7 @@ describe('Acceptance | Run | Slack', function () {
ok: true,
user: {
profile: {
email: 'pixou@pix.fr',
email: 'john.doe@pix.fr',
},
},
};
Expand Down Expand Up @@ -386,7 +388,7 @@ describe('Acceptance | Run | Slack', function () {
},
callback_id: 'application-creation-confirmation',
private_metadata:
'{"applicationName":"pix-application-de-folie-recette","applicationEnvironment":"recette","userEmail":"pixou@pix.fr"}',
'{"applicationName":"pix-application-de-folie-recette","applicationEnvironment":"recette","userEmail":"john.doe@pix.fr"}',
close: {
type: 'plain_text',
text: 'Annuler',
Expand All @@ -395,7 +397,7 @@ describe('Acceptance | Run | Slack', function () {
{
text: {
type: 'mrkdwn',
text: "Vous vous apprêtez à créer l'application *pix-application-de-folie-recette* en *recette* avec pixou@pix.fr en collab. ",
text: "Vous vous apprêtez à créer l'application *pix-application-de-folie-recette* en *recette* et à inviter cet adesse email en tant que collaborateur : *john.doe@pix.fr*",
},
type: 'section',
},
Expand Down
57 changes: 43 additions & 14 deletions test/unit/common/models/ScalingoAppName_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,65 @@ const { expect } = require('../../../test-helper');
const { ScalingoAppName } = require('../../../../common/models/ScalingoAppName');

describe('Unit | Common | Models | ScalingoAppName', function () {
describe('#isValidAppName', function () {
it('should return false if appName is not a valid appName', function () {
describe('#isApplicationNameValid', function () {
it('should return false if appName contains non alphanum or dash char', function () {
// given
const invalidAppName = 'pix#-application-nameat-production';

// when
const resultInvalidAppName = ScalingoAppName.isApplicationNameValid(invalidAppName);
console.log(resultInvalidAppName);
// then
expect(resultInvalidAppName, 'Invalid app name').to.be.false;
});

it('should return false if appName does not start with pix', function () {
// given
const invalidAppName = '_"zz-fail-pix-app-name-format';
const doesNotStartWithPixAppName = 'app-name-format';
const tooShortAppName = 'pix-a';
const tooLongAppName = 'pix-application-with-a-long-name-that-does-not-fit-production';
const incorrectSuffixAppName = 'pix-coucou-app-name-mauvaissuffix';

// when
const resultInvalidAppName = ScalingoAppName.isValidAppName(invalidAppName);
const resultDoesNotStartWithPixAppName = ScalingoAppName.isValidAppName(doesNotStartWithPixAppName);
const resultTooShortAppName = ScalingoAppName.isValidAppName(tooShortAppName);
const resultTooLongAppName = ScalingoAppName.isValidAppName(tooLongAppName);
const resultIncorrectSuffixAppName = ScalingoAppName.isValidAppName(incorrectSuffixAppName);
const resultDoesNotStartWithPixAppName = ScalingoAppName.isApplicationNameValid(doesNotStartWithPixAppName);

// then
expect(resultInvalidAppName, 'Invalid app name').to.be.false;
expect(resultDoesNotStartWithPixAppName, 'Does not start with pix app name').to.be.false;
});

it('should return false if appName is too short', function () {
// given
const tooShortAppName = 'pix-a';

// when
const resultTooShortAppName = ScalingoAppName.isApplicationNameValid(tooShortAppName);

// then
expect(resultTooShortAppName, 'Too short app name').to.be.false;
});
it('should return false if appName is too long', function () {
// given
const tooLongAppName = 'pix-application-with-a-long-name-that-does-not-fit-production';

// when
const resultTooLongAppName = ScalingoAppName.isApplicationNameValid(tooLongAppName);

// then
expect(resultTooLongAppName, 'Too long app name').to.be.false;
expect(resultIncorrectSuffixAppName, 'Incorrect suffix app name').to.be.false;
});
it('should return false if appName end with incorrect suffix', function () {
// given
const incorrectSuffixAppName = 'pix-coucou-app-name-mauvaissuffix';

// when
const resultIncorrectSuffixAppName = ScalingoAppName.isApplicationNameValid(incorrectSuffixAppName);

// then
expect(resultIncorrectSuffixAppName, 'Incorrect suffix app name').to.be.false;
});
it('should return true if parameter is a valid appName', function () {
// given
const validAppName = 'pix-super-application-recette';

// when
const result = ScalingoAppName.isValidAppName(validAppName);
const result = ScalingoAppName.isApplicationNameValid(validAppName);

// then
expect(result).to.be.true;
Expand Down

0 comments on commit e7351d3

Please sign in to comment.