|
| 1 | +import { afterEach, beforeEach, describe, it } from "node:test"; |
| 2 | +import type { TestContext } from "node:test"; |
| 3 | + |
| 4 | +import { cleanDatabase } from "../../../tests/helpers"; |
| 5 | +import { |
| 6 | + createAccount, |
| 7 | + createOAuthApplication, |
| 8 | + getAccessToken, |
| 9 | +} from "../../../tests/helpers/oauth"; |
| 10 | + |
| 11 | +import app from "../../index"; |
| 12 | + |
| 13 | +describe("/api/v1/accounts/", () => { |
| 14 | + let client: Awaited<ReturnType<typeof createOAuthApplication>>; |
| 15 | + let account: Awaited<ReturnType<typeof createAccount>>; |
| 16 | + |
| 17 | + beforeEach(async () => { |
| 18 | + account = await createAccount(); |
| 19 | + client = await createOAuthApplication({ |
| 20 | + scopes: ["read:accounts", "write"], |
| 21 | + }); |
| 22 | + }); |
| 23 | + |
| 24 | + afterEach(async () => { |
| 25 | + await cleanDatabase(); |
| 26 | + }); |
| 27 | + |
| 28 | + describe("verify_credentials", () => { |
| 29 | + it("Successfully returns the current accounts profile with a valid access token", async (t: TestContext) => { |
| 30 | + t.plan(7); |
| 31 | + |
| 32 | + const accessToken = await getAccessToken(client, account, [ |
| 33 | + "read:accounts", |
| 34 | + ]); |
| 35 | + |
| 36 | + const response = await app.request( |
| 37 | + "/api/v1/accounts/verify_credentials", |
| 38 | + { |
| 39 | + method: "GET", |
| 40 | + headers: { |
| 41 | + Authorization: accessToken.authorizationHeader, |
| 42 | + }, |
| 43 | + }, |
| 44 | + ); |
| 45 | + |
| 46 | + t.assert.equal(response.status, 200); |
| 47 | + t.assert.equal(response.headers.get("content-type"), "application/json"); |
| 48 | + t.assert.equal(response.headers.get("access-control-allow-origin"), "*"); |
| 49 | + |
| 50 | + const credentialAccount = await response.json(); |
| 51 | + |
| 52 | + t.assert.equal(typeof credentialAccount, "object"); |
| 53 | + t.assert.equal(credentialAccount.id, account.id); |
| 54 | + t.assert.equal(credentialAccount.username, "hollo"); |
| 55 | + t.assert.equal(credentialAccount.acct, "hollo@hollo.test"); |
| 56 | + }); |
| 57 | + |
| 58 | + it("does not return the account when an invalid scope is used", async (t: TestContext) => { |
| 59 | + t.plan(4); |
| 60 | + |
| 61 | + const accessToken = await getAccessToken(client, account, ["write"]); |
| 62 | + |
| 63 | + const response = await app.request( |
| 64 | + "/api/v1/accounts/verify_credentials", |
| 65 | + { |
| 66 | + method: "GET", |
| 67 | + headers: { |
| 68 | + Authorization: accessToken.authorizationHeader, |
| 69 | + }, |
| 70 | + }, |
| 71 | + ); |
| 72 | + |
| 73 | + t.assert.equal(response.status, 403); |
| 74 | + t.assert.equal(response.headers.get("content-type"), "application/json"); |
| 75 | + t.assert.equal(response.headers.get("access-control-allow-origin"), "*"); |
| 76 | + |
| 77 | + const error = await response.json(); |
| 78 | + |
| 79 | + t.assert.deepStrictEqual(error, { |
| 80 | + error: "insufficient_scope", |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + it("does not return the account when no access token is used", async (t: TestContext) => { |
| 85 | + t.plan(4); |
| 86 | + |
| 87 | + const response = await app.request( |
| 88 | + "/api/v1/accounts/verify_credentials", |
| 89 | + { |
| 90 | + method: "GET", |
| 91 | + }, |
| 92 | + ); |
| 93 | + |
| 94 | + t.assert.equal(response.status, 401); |
| 95 | + t.assert.equal(response.headers.get("content-type"), "application/json"); |
| 96 | + t.assert.equal(response.headers.get("access-control-allow-origin"), "*"); |
| 97 | + |
| 98 | + const error = await response.json(); |
| 99 | + |
| 100 | + t.assert.deepStrictEqual(error, { |
| 101 | + error: "unauthorized", |
| 102 | + }); |
| 103 | + }); |
| 104 | + }); |
| 105 | +}); |
0 commit comments