Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('updates a permission', async flags => {
// cleanup
flags.onCleanup = function() {
updateStub.restore();
};
// setup
const fakePermission = { id: 1, action: 'create', resourceId: 10 };
const entity = { description: 'potato' };
const updateStub = Sinon.stub(PermissionService, 'update');
updateStub.withArgs(fakePermission.id, entity).resolves(Hoek.merge(fakePermission, entity));
server.route({
method: 'PUT',
path: '/permission/{id}',
handler: PermissionCtrl.update
});
// exercise
const response = await server.inject({
method: 'PUT',
url: `/permission/${fakePermission.id}`,
payload: entity
});
// validate
expect(updateStub.calledOnce).to.be.true();
sentryEvent.level = event.channel;
// use request credentials for capturing user
if (opts.trackUser) sentryEvent.user = request.auth && request.auth.credentials;
if (sentryEvent.user) {
Object.keys(sentryEvent.user) // hide credentials
.filter(prop => /^(p(ass)?w(or)?(d|t)?|secret)?$/i.test(prop))
.forEach(prop => delete sentryEvent.user[prop]);
}
// some SDK identificator
sentryEvent.sdk = { name: 'sentry.javascript.node.hapi', version };
return sentryEvent;
});
Hoek.merge(scope, request.sentryScope);
Sentry.captureException(event.error);
});
});
if (protocol !== 'https' &&
settings.isSecure) {
return h.unauthenticated(Boom.internal('Invalid setting - isSecure must be set to false for non-https server'), { credentials });
}
// Sign-in Initialization
if (!request.query.code) {
credentials.query = request.query;
const nonce = Cryptiles.randomString(internals.nonceLength);
const query = internals.resolveProviderParams(request, settings.providerParams);
if (settings.allowRuntimeProviderParams) {
Hoek.merge(query, request.query);
}
query.client_id = settings.clientId;
query.response_type = 'code';
query.redirect_uri = internals.location(request, protocol, settings.location);
query.state = nonce;
if (settings.runtimeStateCallback) {
const runtimeState = settings.runtimeStateCallback(request);
if (runtimeState) {
query.state += runtimeState;
}
}
let scope = settings.scope || settings.provider.scope;
if (typeof scope === 'function') {
.where('username', entity.username)
.orWhere('email', entity.email)
: Promise.resolve([]);
const [user, equalUsers] = await Promise.all([getUserByID, getEqualUsers]);
if (!user) {
throw NSError.RESOURCE_NOT_FOUND();
}
// username or email is already taken, we should not update
if (equalUsers.length > 0 && equalUsers.some(equalUser => equalUser.id !== user.id)) {
throw NSError.RESOURCE_DUPLICATE();
}
Hoek.merge(user, Hoek.clone(entity, { shallow: Repository.User.model.updateFields }));
if (entity.password) {
user.password = await Auth.crypt(entity.password);
}
return txUserRepository.update(user);
});
};
it('authenticates with mock', async (flags) => {
const mock = await Mock.v2(flags);
const server = Hapi.server({ host: 'localhost', port: 80 });
await server.register(Bell);
const custom = Bell.providers.bitbucket();
Hoek.merge(custom, mock.provider);
Mock.override('https://api.bitbucket.org/2.0/user', {
repositories: [{}],
uuid: '1E9C5160-E436-11E5-9897-4FCB70D5A8C7',
username: 'steve',
display_name: 'steve'
});
server.auth.strategy('custom', 'bell', {
password: 'cookie_encryption_password_secure',
isSecure: false,
clientId: 'bitbucket',
clientSecret: 'secret',
provider: custom
});
it('authenticates with mock', async (flags) => {
const mock = await Mock.v2(flags);
const server = Hapi.server({ host: 'localhost', port: 80 });
await server.register(Bell);
const custom = Bell.providers.linkedin();
Hoek.merge(custom, mock.provider);
const profile = {
id: '1234567890',
firstName: {
localized: {
en_US: 'steve'
},
preferredLocal: {
country: 'US',
language: 'en'
}
},
lastName: {
localized: {
en_US: 'smith'
},
it('authenticates with mock (without extended profile)', async (flags) => {
const mock = await Mock.v2(flags);
const server = Hapi.server({ host: 'localhost', port: 80 });
await server.register(Bell);
const custom = Bell.providers.slack({ extendedProfile: false });
Hoek.merge(custom, mock.provider);
server.auth.strategy('custom', 'bell', {
password: 'cookie_encryption_password_secure',
isSecure: false,
clientId: 'slack',
clientSecret: 'secret',
provider: custom
});
server.route({
method: '*',
path: '/login',
config: {
auth: 'custom',
handler: function (request, h) {
it('errors on failed token request', async (flags) => {
const mock = await Mock.v2(flags);
const server = Hapi.server({ host: 'localhost', port: 8080 });
await server.register(Bell);
const custom = Bell.providers.facebook();
Hoek.merge(custom, mock.provider);
Mock.override(mock.provider.token, null);
server.auth.strategy('custom', 'bell', {
password: 'cookie_encryption_password_secure',
isSecure: false,
clientId: 'facebook',
clientSecret: 'secret',
provider: custom
});
server.route({
method: '*',
path: '/login',
options: {
auth: 'custom',
results.forEach((result) => {
Hoek.merge(finalData, result)
})
const register = async function(server) {
Hoek.assert(server.settings.app.name);
const options = Hoek.merge(internals.options, { name: server.settings.app.name });
await server.register({ plugin: HapiPino, options });
server.events.on('route', event => {
server
.logger()
.child({ method: event.method, path: event.path })
.debug('route');
});
server
.logger()
.child({ plugin: exports.plugin.name })
.debug('plugin');
};