From f42b636adceaf348ba1a6997ac04124e15500be7 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Wed, 10 May 2023 15:57:46 -0400 Subject: [PATCH] test: fix authentication tests to use new token saving utility functions --- src/meta/configs.js | 2 -- src/plugins/index.js | 1 - src/routes/authentication.js | 1 - test/authentication.js | 25 ++++++++----------------- 4 files changed, 8 insertions(+), 21 deletions(-) diff --git a/src/meta/configs.js b/src/meta/configs.js index ca53dc1494..d1ef52e531 100644 --- a/src/meta/configs.js +++ b/src/meta/configs.js @@ -7,8 +7,6 @@ const winston = require('winston'); const db = require('../database'); const pubsub = require('../pubsub'); -const plugins = require('../plugins'); -const utils = require('../utils'); const Meta = require('./index'); const cacheBuster = require('./cacheBuster'); const defaults = require('../../install/data/defaults.json'); diff --git a/src/plugins/index.js b/src/plugins/index.js index f573202450..a88d7b682e 100644 --- a/src/plugins/index.js +++ b/src/plugins/index.js @@ -10,7 +10,6 @@ const request = require('request-promise-native'); const user = require('../user'); const posts = require('../posts'); -const meta = require('../meta'); const { pluginNamePattern, themeNamePattern, paths } = require('../constants'); diff --git a/src/routes/authentication.js b/src/routes/authentication.js index 74c778b02e..8d5bee679e 100644 --- a/src/routes/authentication.js +++ b/src/routes/authentication.js @@ -6,7 +6,6 @@ const passportLocal = require('passport-local').Strategy; const BearerStrategy = require('passport-http-bearer').Strategy; const winston = require('winston'); -const meta = require('../meta'); const controllers = require('../controllers'); const helpers = require('../controllers/helpers'); const plugins = require('../plugins'); diff --git a/test/authentication.js b/test/authentication.js index 7bb99687ee..60be72e457 100644 --- a/test/authentication.js +++ b/test/authentication.js @@ -15,6 +15,7 @@ const utils = require('../src/utils'); const meta = require('../src/meta'); const plugins = require('../src/plugins'); const privileges = require('../src/privileges'); +const api = require('../src/api'); const helpers = require('./helpers'); describe('authentication', () => { @@ -596,24 +597,14 @@ describe('authentication', () => { let masterToken; before(async () => { newUid = await user.create({ username: 'apiUserTarget' }); - const settings = await meta.settings.get('core.api'); - settings.tokens = settings.tokens || []; - userToken = { - token: utils.generateUUID(), + userToken = await api.utils.tokens.generate({ uid: newUid, description: `api token for uid ${newUid}`, - timestamp: Date.now(), - }; - settings.tokens.push(userToken); - masterToken = { - token: utils.generateUUID(), + }); + masterToken = await api.utils.tokens.generate({ uid: 0, 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 () => { @@ -635,7 +626,7 @@ describe('authentication', () => { const { res, body } = await helpers.request('get', `/api/self`, { json: true, headers: { - Authorization: `Bearer ${userToken.token}`, + Authorization: `Bearer ${userToken}`, }, }); @@ -648,7 +639,7 @@ describe('authentication', () => { form: {}, json: true, headers: { - Authorization: `Bearer ${masterToken.token}`, + Authorization: `Bearer ${masterToken}`, }, }); @@ -663,7 +654,7 @@ describe('authentication', () => { }, json: true, headers: { - Authorization: `Bearer ${masterToken.token}`, + Authorization: `Bearer ${masterToken}`, }, });