Skip to content
This repository has been archived by the owner on Jul 12, 2020. It is now read-only.

Commit

Permalink
better creds injection for tests (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
jedireza authored Feb 8, 2018
1 parent 415b8bb commit d5b48c9
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 214 deletions.
64 changes: 19 additions & 45 deletions test/server/api/accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ const User = require('../../../server/models/user');

const lab = exports.lab = Lab.script();
let server;
let rootAuthHeader;
let adminAuthHeader;
let accountAuthHeader;
let rootCredentials;
let adminCredentials;
let accountCredentials;


lab.before(async () => {
Expand All @@ -38,15 +38,11 @@ lab.before(async () => {
await server.start();
await Fixtures.Db.removeAllData();

const [root, admin, account] = await Promise.all([
[rootCredentials, adminCredentials, accountCredentials] = await Promise.all([
Fixtures.Creds.createRootAdminUser(),
Fixtures.Creds.createAdminUser('Ren Hoek', 'ren', 'baddog', '[email protected]'),
Fixtures.Creds.createAccountUser('Stimpson Cat', 'stimpy', 'goodcat', '[email protected]')
]);

rootAuthHeader = root.authHeader;
adminAuthHeader = admin.authHeader;
accountAuthHeader = account.authHeader;
});


Expand All @@ -67,9 +63,7 @@ lab.experiment('GET /api/accounts', () => {
request = {
method: 'GET',
url: '/api/accounts',
headers: {
authorization: adminAuthHeader
}
credentials: adminCredentials
};
});

Expand All @@ -96,9 +90,7 @@ lab.experiment('POST /api/accounts', () => {
request = {
method: 'POST',
url: '/api/accounts',
headers: {
authorization: adminAuthHeader
}
credentials: adminCredentials
};
});

Expand Down Expand Up @@ -129,9 +121,7 @@ lab.experiment('GET /api/accounts/{id}', () => {
request = {
method: 'GET',
url: '/api/accounts/{id}',
headers: {
authorization: adminAuthHeader
}
credentials: adminCredentials
};
});

Expand Down Expand Up @@ -173,9 +163,7 @@ lab.experiment('PUT /api/accounts/{id}', () => {
request = {
method: 'PUT',
url: '/api/accounts/{id}',
headers: {
authorization: adminAuthHeader
}
credentials: adminCredentials
};
});

Expand Down Expand Up @@ -233,9 +221,7 @@ lab.experiment('DELETE /api/accounts/{id}', () => {
request = {
method: 'DELETE',
url: '/api/accounts/{id}',
headers: {
authorization: rootAuthHeader
}
credentials: rootCredentials
};
});

Expand Down Expand Up @@ -276,9 +262,7 @@ lab.experiment('PUT /api/accounts/{id}/user', () => {
request = {
method: 'PUT',
url: '/api/accounts/{id}/user',
headers: {
authorization: adminAuthHeader
}
credentials: adminCredentials
};
});

Expand Down Expand Up @@ -315,7 +299,7 @@ lab.experiment('PUT /api/accounts/{id}/user', () => {

lab.test('it returns HTTP 409 when the user is linked to another account', async () => {

const { account: accountA } = await Fixtures.Creds.createAccountUser(
const { roles: { account: accountA } } = await Fixtures.Creds.createAccountUser(
'Trevor Noah', 'trevor', 'haha', '[email protected]'
);

Expand All @@ -337,7 +321,7 @@ lab.experiment('PUT /api/accounts/{id}/user', () => {

lab.test('it returns HTTP 409 when the account is currently linked to user', async () => {

const { account } = await Fixtures.Creds.createAccountUser(
const { roles: { account: account } } = await Fixtures.Creds.createAccountUser(
'Mr Horse', 'mrh', 'negh', '[email protected]'
);

Expand Down Expand Up @@ -386,9 +370,7 @@ lab.experiment('DELETE /api/accounts/{id}/user', () => {
request = {
method: 'DELETE',
url: '/api/accounts/{id}/user',
headers: {
authorization: adminAuthHeader
}
credentials: adminCredentials
};
});

Expand Down Expand Up @@ -422,7 +404,7 @@ lab.experiment('DELETE /api/accounts/{id}/user', () => {

lab.test('it returns HTTP 404 when `User.findById` misses', async () => {

const { account, user } = await Fixtures.Creds.createAccountUser(
const { roles: { account: account }, user } = await Fixtures.Creds.createAccountUser(
'Lil Horse', 'lilh', 'negh', '[email protected]'
);

Expand All @@ -439,7 +421,7 @@ lab.experiment('DELETE /api/accounts/{id}/user', () => {

lab.test('it returns HTTP 200 when all is good', async () => {

const { account, user } = await Fixtures.Creds.createAccountUser(
const { roles: { account: account }, user } = await Fixtures.Creds.createAccountUser(
'Jr Horse', 'jrh', 'negh', '[email protected]'
);

Expand Down Expand Up @@ -473,9 +455,7 @@ lab.experiment('POST /api/accounts/{id}/notes', () => {
request = {
method: 'POST',
url: '/api/accounts/{id}/notes',
headers: {
authorization: adminAuthHeader
}
credentials: adminCredentials
};
});

Expand Down Expand Up @@ -525,9 +505,7 @@ lab.experiment('POST /api/accounts/{id}/status', () => {
request = {
method: 'POST',
url: '/api/accounts/{id}/status',
headers: {
authorization: adminAuthHeader
}
credentials: adminCredentials
};
});

Expand Down Expand Up @@ -599,9 +577,7 @@ lab.experiment('GET /api/accounts/my', () => {
request = {
method: 'GET',
url: '/api/accounts/my',
headers: {
authorization: accountAuthHeader
}
credentials: accountCredentials
};
});

Expand All @@ -627,9 +603,7 @@ lab.experiment('PUT /api/accounts/my', () => {
request = {
method: 'PUT',
url: '/api/accounts/my',
headers: {
authorization: accountAuthHeader
}
credentials: accountCredentials
};
});

Expand Down
30 changes: 8 additions & 22 deletions test/server/api/admin-groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const Manifest = require('../../../manifest');

const lab = exports.lab = Lab.script();
let server;
let rootAuthHeader;
let rootCredentials;


lab.before(async () => {
Expand All @@ -34,9 +34,7 @@ lab.before(async () => {
await server.start();
await Fixtures.Db.removeAllData();

const root = await Fixtures.Creds.createRootAdminUser();

rootAuthHeader = root.authHeader;
rootCredentials = await Fixtures.Creds.createRootAdminUser();
});


Expand All @@ -57,9 +55,7 @@ lab.experiment('GET /api/admin-groups', () => {
request = {
method: 'GET',
url: '/api/admin-groups',
headers: {
authorization: rootAuthHeader
}
credentials: rootCredentials
};
});

Expand All @@ -86,9 +82,7 @@ lab.experiment('POST /api/admin-groups', () => {
request = {
method: 'POST',
url: '/api/admin-groups',
headers: {
authorization: rootAuthHeader
}
credentials: rootCredentials
};
});

Expand Down Expand Up @@ -118,9 +112,7 @@ lab.experiment('GET /api/admin-groups/{id}', () => {
request = {
method: 'GET',
url: '/api/admin-groups/{id}',
headers: {
authorization: rootAuthHeader
}
credentials: rootCredentials
};
});

Expand Down Expand Up @@ -161,9 +153,7 @@ lab.experiment('PUT /api/admin-groups/{id}', () => {
request = {
method: 'PUT',
url: '/api/admin-groups/{id}',
headers: {
authorization: rootAuthHeader
}
credentials: rootCredentials
};
});

Expand Down Expand Up @@ -210,9 +200,7 @@ lab.experiment('DELETE /api/admin-groups/{id}', () => {
request = {
method: 'DELETE',
url: '/api/admin-groups/{id}',
headers: {
authorization: rootAuthHeader
}
credentials: rootCredentials
};
});

Expand Down Expand Up @@ -253,9 +241,7 @@ lab.experiment('PUT /api/admin-groups/{id}/permissions', () => {
request = {
method: 'PUT',
url: '/api/admin-groups/{id}/permissions',
headers: {
authorization: rootAuthHeader
}
credentials: rootCredentials
};
});

Expand Down
Loading

0 comments on commit d5b48c9

Please sign in to comment.