Skip to content

Commit

Permalink
test(api): factor out server calls in token.route acceptance tests
Browse files Browse the repository at this point in the history
by using a _getPostFormOptions helper function.
  • Loading branch information
lego-technix committed Feb 26, 2025
1 parent 197fed0 commit 07e685c
Showing 1 changed file with 75 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,16 @@ describe('Acceptance | Identity Access Management | Route | Token', function ()
});

it('returns a 200 with an access token and a refresh token when authentication is ok', async function () {
// given / when
const response = await server.inject({
method: 'POST',
// given
const options = _getPostFormOptions({
url: '/api/token',
headers: {
'content-type': 'application/x-www-form-urlencoded',
'x-forwarded-proto': 'https',
'x-forwarded-host': 'orga.pix.fr',
},
payload: querystring.stringify({
grant_type: 'password',
username: userEmailAddress,
password: userPassword,
}),
dataToPost: { grant_type: 'password', username: userEmailAddress, password: userPassword },
applicationName: 'orga',
});

// when
const response = await server.inject(options);

// then
const result = response.result;
expect(response.statusCode).to.equal(200);
Expand All @@ -72,22 +66,15 @@ describe('Acceptance | Identity Access Management | Route | Token', function ()

await databaseBuilder.commit();

// when
const response = await server.inject({
method: 'POST',
const options = _getPostFormOptions({
url: '/api/token',
headers: {
'content-type': 'application/x-www-form-urlencoded',
'x-forwarded-proto': 'https',
'x-forwarded-host': 'orga.pix.fr',
},
payload: querystring.stringify({
grant_type: 'password',
username: 'beth.rave1212',
password: userPassword,
}),
dataToPost: { grant_type: 'password', username: 'beth.rave1212', password: userPassword },
applicationName: 'orga',
});

// when
const response = await server.inject(options);

// then
expect(response.statusCode).to.equal(401);
expect(response.result.errors[0].title).equal('PasswordShouldChange');
Expand All @@ -97,36 +84,29 @@ describe('Acceptance | Identity Access Management | Route | Token', function ()
context('when user needs to refresh his access token', function () {
it('returns a 200 with a new access token', async function () {
// given
const { result: accessTokenResult } = await server.inject({
method: 'POST',
const optionsForAccessToken = _getPostFormOptions({
url: '/api/token',
headers: {
'content-type': 'application/x-www-form-urlencoded',
'x-forwarded-proto': 'https',
'x-forwarded-host': 'orga.pix.fr',
},
payload: querystring.stringify({
dataToPost: {
grant_type: 'password',
username: userEmailAddress,
password: userPassword,
}),
},
applicationName: 'orga',
});
const { result: accessTokenResult } = await server.inject(optionsForAccessToken);

// when
const response = await server.inject({
method: 'POST',
const options = _getPostFormOptions({
url: '/api/token',
headers: {
'content-type': 'application/x-www-form-urlencoded',
'x-forwarded-proto': 'https',
'x-forwarded-host': 'orga.pix.fr',
},
payload: querystring.stringify({
dataToPost: {
grant_type: 'refresh_token',
refresh_token: accessTokenResult.refresh_token,
}),
},
applicationName: 'orga',
});

// when
const response = await server.inject(options);

// then
const result = response.result;
expect(response.statusCode).to.equal(200);
Expand All @@ -144,7 +124,7 @@ describe('Acceptance | Identity Access Management | Route | Token', function ()
context('when requestedApplication is admin', function () {
context('when admin member has allowed role but has been disabled', function () {
it('returns http code 403', async function () {
//given
// given
const user = databaseBuilder.factory.buildUser.withRawPassword({
email: '[email protected]',
rawPassword: userPassword,
Expand All @@ -157,7 +137,11 @@ describe('Acceptance | Identity Access Management | Route | Token', function ()
});
await databaseBuilder.commit();

const options = _getServerOptions({ username: user.email, password: userPassword, applicationName: 'admin' });
const options = _getPostFormOptions({
url: '/api/token',
dataToPost: { grant_type: 'password', username: user.email, password: userPassword },
applicationName: 'admin',
});

// when
const response = await server.inject(options);
Expand All @@ -170,21 +154,20 @@ describe('Acceptance | Identity Access Management | Route | Token', function ()

context('when application is Pix Certif', function () {
it('returns http code 200 with accessToken when authentication is ok', async function () {
//given
// given
databaseBuilder.factory.buildCertificationCenter({ id: 345 });
databaseBuilder.factory.buildSession({ id: 121, certificationCenterId: 345 });
const candidate = databaseBuilder.factory.buildCertificationCandidate({ sessionId: 121 });
databaseBuilder.factory.buildCoreSubscription({ certificationCandidateId: candidate.id });
databaseBuilder.factory.buildSupervisorAccess({ userId, sessionId: 121 });
await databaseBuilder.commit();

const options = _getServerOptions({
username: userEmailAddress,
password: userPassword,
const options = _getPostFormOptions({
url: '/api/token',
dataToPost: { grant_type: 'password', username: userEmailAddress, password: userPassword },
applicationName: 'certif',
});

await databaseBuilder.commit();
// when
const response = await server.inject(options);

Expand Down Expand Up @@ -214,9 +197,9 @@ describe('Acceptance | Identity Access Management | Route | Token', function ()
databaseBuilder.factory.buildUserLogin({ userId, failureCount: 9 });
await databaseBuilder.commit();

const options = _getServerOptions({
username: '[email protected]',
password: 'wrongPassword',
const options = _getPostFormOptions({
url: '/api/token',
dataToPost: { grant_type: 'password', username: '[email protected]', password: 'wrongPassword' },
applicationName: 'app',
});

Expand Down Expand Up @@ -246,9 +229,9 @@ describe('Acceptance | Identity Access Management | Route | Token', function ()
});
await databaseBuilder.commit();

const options = _getServerOptions({
username: '[email protected]',
password: userPassword,
const options = _getPostFormOptions({
url: '/api/token',
dataToPost: { grant_type: 'password', username: '[email protected]', password: userPassword },
applicationName: 'app',
});

Expand All @@ -275,9 +258,9 @@ describe('Acceptance | Identity Access Management | Route | Token', function ()
});
await databaseBuilder.commit();

const options = _getServerOptions({
username: '[email protected]',
password: userPassword,
const options = _getPostFormOptions({
url: '/api/token',
dataToPost: { grant_type: 'password', username: '[email protected]', password: userPassword },
applicationName: 'app',
});

Expand Down Expand Up @@ -306,23 +289,16 @@ describe('Acceptance | Identity Access Management | Route | Token', function ()
});
await databaseBuilder.commit();

// when
const response = await server.inject({
method: 'POST',
const options = _getPostFormOptions({
url: '/api/token',
headers: {
'content-type': 'application/x-www-form-urlencoded',
cookie: `locale=${localeFromCookie}`,
'x-forwarded-proto': 'https',
'x-forwarded-host': 'app.pix.fr',
},
payload: querystring.stringify({
grant_type: 'password',
username: userWithoutLocale.email,
password: userPassword,
}),
dataToPost: { grant_type: 'password', username: userWithoutLocale.email, password: userPassword },
applicationName: 'app',
localeFromCookie,
});

// when
const response = await server.inject(options);

// then
expect(response.statusCode).to.equal(200);
const user = await knex('users').where({ id: userWithoutLocale.id }).first();
Expand All @@ -343,23 +319,20 @@ describe('Acceptance | Identity Access Management | Route | Token', function ()
});
await databaseBuilder.commit();

// when
const response = await server.inject({
method: 'POST',
const options = _getPostFormOptions({
url: '/api/token',
headers: {
'content-type': 'application/x-www-form-urlencoded',
cookie: `locale=${localeFromCookie}`,
'x-forwarded-proto': 'https',
'x-forwarded-host': 'app.pix.fr',
},
payload: querystring.stringify({
dataToPost: {
grant_type: 'password',
username: userWithLocale.email,
password: userPassword,
}),
},
applicationName: 'app',
localeFromCookie,
});

// when
const response = await server.inject(options);

// then
expect(response.statusCode).to.equal(200);
const user = await knex('users').where({ id: userWithLocale.id }).first();
Expand All @@ -380,22 +353,16 @@ describe('Acceptance | Identity Access Management | Route | Token', function ()
beforeEach(async function () {
const targetProfile = databaseBuilder.factory.buildTargetProfile({ isSimplifiedAccess: false });
databaseBuilder.factory.buildCampaign({ code: campaignCode, targetProfile });
await databaseBuilder.commit();

options = {
method: 'POST',
options = _getPostFormOptions({
url: '/api/token/anonymous',
headers: {
'content-type': 'application/x-www-form-urlencoded',
'x-forwarded-proto': 'https',
'x-forwarded-host': 'app.pix.fr',
},
payload: querystring.stringify({
dataToPost: {
campaign_code: campaignCode,
lang,
}),
};

await databaseBuilder.commit();
},
applicationName: 'app',
});
});

it('returns an 401', async function () {
Expand All @@ -418,22 +385,16 @@ describe('Acceptance | Identity Access Management | Route | Token', function ()
beforeEach(async function () {
const targetProfileId = databaseBuilder.factory.buildTargetProfile({ isSimplifiedAccess: true }).id;
databaseBuilder.factory.buildCampaign({ code: simplifiedAccessCampaignCode, targetProfileId });
await databaseBuilder.commit();

options = {
method: 'POST',
options = _getPostFormOptions({
url: '/api/token/anonymous',
headers: {
'content-type': 'application/x-www-form-urlencoded',
'x-forwarded-proto': 'https',
'x-forwarded-host': 'app.pix.fr',
},
payload: querystring.stringify({
dataToPost: {
campaign_code: simplifiedAccessCampaignCode,
lang,
}),
};

await databaseBuilder.commit();
},
applicationName: 'app',
});
});

it('returns a 200 with accessToken', async function () {
Expand Down Expand Up @@ -540,19 +501,16 @@ describe('Acceptance | Identity Access Management | Route | Token', function ()
});
});

function _getServerOptions({ username, password, applicationName }) {
function _getPostFormOptions({ url, dataToPost, applicationName, localeFromCookie }) {
return {
method: 'POST',
url: '/api/token',
url,
headers: {
'content-type': 'application/x-www-form-urlencoded',
'x-forwarded-proto': 'https',
'x-forwarded-host': `${applicationName}.pix.fr`,
...(localeFromCookie && { cookie: `locale=${localeFromCookie}` }),
},
payload: querystring.stringify({
grant_type: 'password',
username,
password,
}),
payload: querystring.stringify(dataToPost),
};
}

0 comments on commit 07e685c

Please sign in to comment.