diff options
author | Vitaly Takmazov | 2024-10-21 06:54:19 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2024-10-21 06:54:19 +0300 |
commit | c6acc5c09ee87d4a4e850733c096d2673b78e1f2 (patch) | |
tree | 40137224aeb3cebfed27488a954e3643920dc2ee | |
parent | 6f7c755dca704ccf880538b7884473c9c829c2b7 (diff) |
vnext: WebFinger tests
-rw-r--r-- | package.json | 2 | ||||
-rw-r--r-- | vnext/server/db/Mastodon.spec.js | 21 | ||||
-rw-r--r-- | vnext/server/db/Users.js | 5 | ||||
-rw-r--r-- | vnext/server/db/__testdata__/testdata.sql | 1 | ||||
-rw-r--r-- | vnext/server/middleware/mastodon.spec.js | 17 | ||||
-rw-r--r-- | vnext/server/middleware/webfinger.js | 17 | ||||
-rw-r--r-- | vnext/server/middleware/webfinger.spec.js | 31 |
7 files changed, 66 insertions, 28 deletions
diff --git a/package.json b/package.json index b31416ab..9488470b 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "juick", "private": true, "scripts": { - "test": "sqlite3 juick.db < src/main/resources/schema-sqlite.sql && cross-env DATABASE_URL=sqlite:juick.db jest && rm juick.db", + "test": "sqlite3 juick.db < src/main/resources/schema-sqlite.sql && sqlite3 juick.db < vnext/server/db/__testdata__/testdata.sql && cross-env DATABASE_URL=sqlite:juick.db DEBUG=webfinger jest && rm juick.db", "watch": "webpack --watch --mode development", "compile": "webpack --mode production", "lint": "eslint src/main/assets", diff --git a/vnext/server/db/Mastodon.spec.js b/vnext/server/db/Mastodon.spec.js deleted file mode 100644 index f2edb40b..00000000 --- a/vnext/server/db/Mastodon.spec.js +++ /dev/null @@ -1,21 +0,0 @@ -import request from 'supertest' -import express from 'express' -import { instance } from '../middleware/mastodon' -import db from './index' - -const app = express() -app.get('/instance', instance) - -describe('Mastodon API middleware', () => { - it('Inactive users should not be included in Instance response', async () => { - await db.query('INSERT INTO users(nick,passw,last_seen) \ - VALUES(\'ugnich\', \'12345\', NULL), \ - (\'freefd\', \'12345\', DATETIME(\'now\'))') - return request(app) - .get('/instance') - .expect(200) - .then(response => { - expect(response.body.usage.users.active_month).toStrictEqual(1) - }) - }) -}) diff --git a/vnext/server/db/Users.js b/vnext/server/db/Users.js index a6148497..3f4422e2 100644 --- a/vnext/server/db/Users.js +++ b/vnext/server/db/Users.js @@ -29,10 +29,13 @@ export const getMonthlyActiveUsers = async () => { } export const getUserByName = async (name = '') => { - return User.findOne({ + return await User.findOne({ where: { nick: { [Op.eq]: name + }, + banned: { + [Op.eq]: 0 } } }) diff --git a/vnext/server/db/__testdata__/testdata.sql b/vnext/server/db/__testdata__/testdata.sql new file mode 100644 index 00000000..f2876732 --- /dev/null +++ b/vnext/server/db/__testdata__/testdata.sql @@ -0,0 +1 @@ +INSERT INTO users(nick,passw,last_seen) VALUES('ugnich', '12345', NULL), ('freefd', '12345', DATETIME('now')) diff --git a/vnext/server/middleware/mastodon.spec.js b/vnext/server/middleware/mastodon.spec.js new file mode 100644 index 00000000..648923e4 --- /dev/null +++ b/vnext/server/middleware/mastodon.spec.js @@ -0,0 +1,17 @@ +import request from 'supertest' +import express from 'express' +import { instance } from './mastodon' + +const app = express() +app.get('/instance', instance) + +describe('Mastodon API middleware', () => { + it('Inactive users should not be included in Instance response', async () => { + return request(app) + .get('/instance') + .expect(200) + .then(response => { + expect(response.body.usage.users.active_month).toStrictEqual(1) + }) + }) +}) diff --git a/vnext/server/middleware/webfinger.js b/vnext/server/middleware/webfinger.js index 9800fc01..873387b3 100644 --- a/vnext/server/middleware/webfinger.js +++ b/vnext/server/middleware/webfinger.js @@ -1,5 +1,7 @@ import config from 'config' import addrparser from 'address-rfc2822' +import debug from 'debug' +var log = debug('webfinger') import { getUserByName } from '../db/Users' @@ -18,7 +20,8 @@ export const webfinger = async (req, res) => { const address = addresses[0] const ourAddress = new URL(baseUrl) if (address.host() === ourAddress.hostname) { - const user = await getUserByName(addresses[0].user()) + const name = addresses[0].user() + const user = await getUserByName(name) if (user) { return res.json({ subject: resource, @@ -31,16 +34,20 @@ export const webfinger = async (req, res) => { ] }) } else { - return res.status(404).send('User not found') + log(`User not found: ${name}`) + return res.status(404).end() } } else { - return res.status(404).send('Address not found') + log(`Address not found: ${address.host()}`) + return res.status(404).end() } } else { - return res.status(400).send('Invalid resource') + log(`Invalid resource: ${resource}`) + return res.status(400).end() } } - res.status(400).send('Missing `resource` param') + log('Missing `resource` param') + res.status(400) } const parseAddress = (address = '') => { diff --git a/vnext/server/middleware/webfinger.spec.js b/vnext/server/middleware/webfinger.spec.js new file mode 100644 index 00000000..fde2e428 --- /dev/null +++ b/vnext/server/middleware/webfinger.spec.js @@ -0,0 +1,31 @@ +import request from 'supertest' +import express from 'express' +import { webfinger } from './webfinger' + +const app = express() +app.get('/webfinger', webfinger) + +describe('WebFinger middleware', () => { + it('Existing user response should have a subject and links', async () => { + const resource = 'acct:ugnich@juick.com' + const response = await request(app) + .get(`/webfinger?resource=${resource}`) + expect(response.status).toStrictEqual(200) + expect(response.body.subject).toStrictEqual(resource) + expect(response.body.links.length).toStrictEqual(1) + expect(response.body.links[0].href).toStrictEqual('https://juick.com/u/ugnich') + }) + it('Unknown user should return 404', async () => { + const resource = 'acct:durov@juick.com' + const response = await request(app) + .get(`/webfinger?resource=${resource}`) + expect(response.status).toStrictEqual(404) + }) + it('Invalid input should return 400', async () => { + const resource = ';DROP TABLE users' + const response = await request(app) + .get(`/webfinger?resource=${resource}`) + expect(response.status).toStrictEqual(400) + }) +}) + |