mirror of
https://github.com/outline/outline.git
synced 2026-12-02 09:42:07 +03:00
test: Add tests for oauthAuthentications endpoints
This commit is contained in:
5 files changed
+228
-9
No files matched your search
+19
@@ -0,0 +1,19 @@
|
||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`oauthAuthentications.delete should require authentication 1`] = `
|
||||
{
|
||||
"error": "authentication_required",
|
||||
"message": "Authentication required",
|
||||
"ok": false,
|
||||
"status": 401,
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`oauthAuthentications.list should require authentication 1`] = `
|
||||
{
|
||||
"error": "authentication_required",
|
||||
"message": "Authentication required",
|
||||
"ok": false,
|
||||
"status": 401,
|
||||
}
|
||||
`;
|
||||
@@ -0,0 +1,194 @@
|
||||
import { OAuthClient, OAuthAuthentication } from "@server/models";
|
||||
import {
|
||||
buildOAuthAuthentication,
|
||||
buildTeam,
|
||||
buildUser,
|
||||
} from "@server/test/factories";
|
||||
import { getTestServer } from "@server/test/support";
|
||||
|
||||
const server = getTestServer();
|
||||
|
||||
describe("oauthAuthentications.list", () => {
|
||||
it("should require authentication", async () => {
|
||||
const res = await server.post("/api/oauthAuthentications.list");
|
||||
const body = await res.json();
|
||||
expect(res.status).toEqual(401);
|
||||
expect(body).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it("should return list of oauth authentications for user", async () => {
|
||||
const team = await buildTeam();
|
||||
const user = await buildUser({ teamId: team.id });
|
||||
const oauthClient = await OAuthClient.create({
|
||||
teamId: team.id,
|
||||
createdById: user.id,
|
||||
name: "Test Client",
|
||||
redirectUris: ["https://example.com/callback"],
|
||||
});
|
||||
|
||||
await buildOAuthAuthentication({
|
||||
oauthClientId: oauthClient.id,
|
||||
user,
|
||||
scope: ["read"],
|
||||
});
|
||||
|
||||
const res = await server.post("/api/oauthAuthentications.list", {
|
||||
body: {
|
||||
token: user.getJwtToken(),
|
||||
},
|
||||
});
|
||||
|
||||
const body = await res.json();
|
||||
expect(res.status).toEqual(200);
|
||||
expect(body.data.length).toEqual(1);
|
||||
expect(body.data[0].id).toBeDefined();
|
||||
expect(body.data[0].oauthClient.name).toEqual("Test Client");
|
||||
expect(body.policies).toBeDefined();
|
||||
});
|
||||
|
||||
it("should only return authentications for requesting user", async () => {
|
||||
const team = await buildTeam();
|
||||
const user = await buildUser({ teamId: team.id });
|
||||
const anotherUser = await buildUser({ teamId: team.id });
|
||||
const oauthClient = await OAuthClient.create({
|
||||
teamId: team.id,
|
||||
createdById: user.id,
|
||||
name: "Test Client",
|
||||
redirectUris: ["https://example.com/callback"],
|
||||
});
|
||||
|
||||
await buildOAuthAuthentication({
|
||||
oauthClientId: oauthClient.id,
|
||||
user: anotherUser,
|
||||
scope: ["read"],
|
||||
});
|
||||
|
||||
const res = await server.post("/api/oauthAuthentications.list", {
|
||||
body: {
|
||||
token: user.getJwtToken(),
|
||||
},
|
||||
});
|
||||
|
||||
const body = await res.json();
|
||||
expect(res.status).toEqual(200);
|
||||
expect(body.data.length).toEqual(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("oauthAuthentications.delete", () => {
|
||||
it("should require authentication", async () => {
|
||||
const res = await server.post("/api/oauthAuthentications.delete");
|
||||
const body = await res.json();
|
||||
expect(res.status).toEqual(401);
|
||||
expect(body).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it("should delete all authentications for a client without scope", async () => {
|
||||
const team = await buildTeam();
|
||||
const user = await buildUser({ teamId: team.id });
|
||||
const oauthClient = await OAuthClient.create({
|
||||
teamId: team.id,
|
||||
createdById: user.id,
|
||||
name: "Test Client",
|
||||
redirectUris: ["https://example.com/callback"],
|
||||
});
|
||||
|
||||
await buildOAuthAuthentication({
|
||||
oauthClientId: oauthClient.id,
|
||||
user,
|
||||
scope: ["read"],
|
||||
});
|
||||
|
||||
const res = await server.post("/api/oauthAuthentications.delete", {
|
||||
body: {
|
||||
token: user.getJwtToken(),
|
||||
oauthClientId: oauthClient.id,
|
||||
},
|
||||
});
|
||||
|
||||
const body = await res.json();
|
||||
expect(res.status).toEqual(200);
|
||||
expect(body.success).toBe(true);
|
||||
|
||||
const auths = await OAuthAuthentication.findAll({
|
||||
where: {
|
||||
userId: user.id,
|
||||
oauthClientId: oauthClient.id,
|
||||
},
|
||||
});
|
||||
expect(auths.length).toEqual(0);
|
||||
});
|
||||
|
||||
it("should delete matching authentications for a client with scope", async () => {
|
||||
const team = await buildTeam();
|
||||
const user = await buildUser({ teamId: team.id });
|
||||
const oauthClient = await OAuthClient.create({
|
||||
teamId: team.id,
|
||||
createdById: user.id,
|
||||
name: "Test Client",
|
||||
redirectUris: ["https://example.com/callback"],
|
||||
});
|
||||
|
||||
await buildOAuthAuthentication({
|
||||
oauthClientId: oauthClient.id,
|
||||
user,
|
||||
scope: ["read"],
|
||||
});
|
||||
await buildOAuthAuthentication({
|
||||
oauthClientId: oauthClient.id,
|
||||
user,
|
||||
scope: ["write"],
|
||||
});
|
||||
|
||||
const res = await server.post("/api/oauthAuthentications.delete", {
|
||||
body: {
|
||||
token: user.getJwtToken(),
|
||||
oauthClientId: oauthClient.id,
|
||||
scope: ["read"],
|
||||
},
|
||||
});
|
||||
|
||||
const body = await res.json();
|
||||
expect(res.status).toEqual(200);
|
||||
expect(body.success).toBe(true);
|
||||
|
||||
const auths = await OAuthAuthentication.findAll({
|
||||
where: {
|
||||
userId: user.id,
|
||||
oauthClientId: oauthClient.id,
|
||||
},
|
||||
});
|
||||
expect(auths.length).toEqual(1);
|
||||
expect(auths[0].scope[0]).toEqual("write");
|
||||
});
|
||||
|
||||
it("should only delete authentications for requesting user", async () => {
|
||||
const team = await buildTeam();
|
||||
const user = await buildUser({ teamId: team.id });
|
||||
const anotherUser = await buildUser({ teamId: team.id });
|
||||
const oauthClient = await OAuthClient.create({
|
||||
teamId: team.id,
|
||||
createdById: user.id,
|
||||
name: "Test Client",
|
||||
redirectUris: ["https://example.com/callback"],
|
||||
});
|
||||
|
||||
const otherAuth = await buildOAuthAuthentication({
|
||||
oauthClientId: oauthClient.id,
|
||||
user: anotherUser,
|
||||
scope: ["read"],
|
||||
});
|
||||
|
||||
await server.post("/api/oauthAuthentications.delete", {
|
||||
body: {
|
||||
token: user.getJwtToken(),
|
||||
oauthClientId: oauthClient.id,
|
||||
scope: "read",
|
||||
},
|
||||
});
|
||||
|
||||
// Verify other user's auth still exists
|
||||
const auth = await OAuthAuthentication.findByPk(otherAuth.id);
|
||||
expect(auth).not.toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -66,12 +66,12 @@ router.post(
|
||||
transaction(),
|
||||
async (ctx: APIContext<T.OAuthAuthenticationsDeleteReq>) => {
|
||||
const { user } = ctx.state.auth;
|
||||
const { oauthClientId, scope } = ctx.request.body;
|
||||
const { oauthClientId, scope } = ctx.input.body;
|
||||
const oauthAuthentications = await OAuthAuthentication.findAll({
|
||||
where: {
|
||||
userId: user.id,
|
||||
oauthClientId,
|
||||
scope,
|
||||
...(scope ? { scope } : {}),
|
||||
},
|
||||
transaction: ctx.state.transaction,
|
||||
});
|
||||
|
||||
@@ -756,15 +756,19 @@ export async function buildOAuthAuthorizationCode(
|
||||
}
|
||||
|
||||
export async function buildOAuthAuthentication({
|
||||
oauthClientId,
|
||||
user,
|
||||
scope,
|
||||
}: {
|
||||
oauthClientId?: string;
|
||||
user: User;
|
||||
scope: string[];
|
||||
}) {
|
||||
const oauthClient = await buildOAuthClient({
|
||||
teamId: user.teamId,
|
||||
});
|
||||
const oauthClient = oauthClientId
|
||||
? await OAuthClient.findByPk(oauthClientId, { rejectOnEmpty: true })
|
||||
: await buildOAuthClient({
|
||||
teamId: user.teamId,
|
||||
});
|
||||
const oauthInterfaceClient = {
|
||||
id: oauthClient.clientId,
|
||||
grants: ["authorization_code"],
|
||||
|
||||
@@ -139,10 +139,12 @@ export const OAuthInterface: RefreshTokenModel &
|
||||
},
|
||||
|
||||
async saveToken(token, client, user) {
|
||||
const accessToken = token.accessToken;
|
||||
const refreshToken = token.refreshToken;
|
||||
const accessTokenExpiresAt = token.accessTokenExpiresAt;
|
||||
const refreshTokenExpiresAt = token.refreshTokenExpiresAt;
|
||||
const {
|
||||
accessToken,
|
||||
refreshToken,
|
||||
accessTokenExpiresAt,
|
||||
refreshTokenExpiresAt,
|
||||
} = token;
|
||||
const accessTokenHash = hash(accessToken);
|
||||
const refreshTokenHash = refreshToken ? hash(refreshToken) : undefined;
|
||||
|
||||
|
||||
Reference in new issue
Block a user