test: fix authentication tests to use new token saving utility functions

isekai-main
Julian Lam 2 years ago
parent 4f524e9f94
commit f42b636adc

@ -7,8 +7,6 @@ const winston = require('winston');
const db = require('../database'); const db = require('../database');
const pubsub = require('../pubsub'); const pubsub = require('../pubsub');
const plugins = require('../plugins');
const utils = require('../utils');
const Meta = require('./index'); const Meta = require('./index');
const cacheBuster = require('./cacheBuster'); const cacheBuster = require('./cacheBuster');
const defaults = require('../../install/data/defaults.json'); const defaults = require('../../install/data/defaults.json');

@ -10,7 +10,6 @@ const request = require('request-promise-native');
const user = require('../user'); const user = require('../user');
const posts = require('../posts'); const posts = require('../posts');
const meta = require('../meta');
const { pluginNamePattern, themeNamePattern, paths } = require('../constants'); const { pluginNamePattern, themeNamePattern, paths } = require('../constants');

@ -6,7 +6,6 @@ const passportLocal = require('passport-local').Strategy;
const BearerStrategy = require('passport-http-bearer').Strategy; const BearerStrategy = require('passport-http-bearer').Strategy;
const winston = require('winston'); const winston = require('winston');
const meta = require('../meta');
const controllers = require('../controllers'); const controllers = require('../controllers');
const helpers = require('../controllers/helpers'); const helpers = require('../controllers/helpers');
const plugins = require('../plugins'); const plugins = require('../plugins');

@ -15,6 +15,7 @@ const utils = require('../src/utils');
const meta = require('../src/meta'); const meta = require('../src/meta');
const plugins = require('../src/plugins'); const plugins = require('../src/plugins');
const privileges = require('../src/privileges'); const privileges = require('../src/privileges');
const api = require('../src/api');
const helpers = require('./helpers'); const helpers = require('./helpers');
describe('authentication', () => { describe('authentication', () => {
@ -596,24 +597,14 @@ describe('authentication', () => {
let masterToken; let masterToken;
before(async () => { before(async () => {
newUid = await user.create({ username: 'apiUserTarget' }); newUid = await user.create({ username: 'apiUserTarget' });
const settings = await meta.settings.get('core.api'); userToken = await api.utils.tokens.generate({
settings.tokens = settings.tokens || [];
userToken = {
token: utils.generateUUID(),
uid: newUid, uid: newUid,
description: `api token for uid ${newUid}`, description: `api token for uid ${newUid}`,
timestamp: Date.now(), });
}; masterToken = await api.utils.tokens.generate({
settings.tokens.push(userToken);
masterToken = {
token: utils.generateUUID(),
uid: 0, uid: 0,
description: 'api master token', description: 'api master token',
timestamp: Date.now(), });
};
settings.tokens.push(masterToken);
await meta.settings.set('core.api', settings);
}); });
it('should fail with invalid token', async () => { it('should fail with invalid token', async () => {
@ -635,7 +626,7 @@ describe('authentication', () => {
const { res, body } = await helpers.request('get', `/api/self`, { const { res, body } = await helpers.request('get', `/api/self`, {
json: true, json: true,
headers: { headers: {
Authorization: `Bearer ${userToken.token}`, Authorization: `Bearer ${userToken}`,
}, },
}); });
@ -648,7 +639,7 @@ describe('authentication', () => {
form: {}, form: {},
json: true, json: true,
headers: { headers: {
Authorization: `Bearer ${masterToken.token}`, Authorization: `Bearer ${masterToken}`,
}, },
}); });
@ -663,7 +654,7 @@ describe('authentication', () => {
}, },
json: true, json: true,
headers: { headers: {
Authorization: `Bearer ${masterToken.token}`, Authorization: `Bearer ${masterToken}`,
}, },
}); });

Loading…
Cancel
Save