-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Improve
withSessionAuth
middleware (#225)
Co-authored-by: Seam Bot <[email protected]>
- Loading branch information
1 parent
2a7cf09
commit 8476d35
Showing
5 changed files
with
140 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import test from "ava" | ||
import jwt from "jsonwebtoken" | ||
|
||
import { | ||
getTestServer, | ||
type SimpleAxiosError, | ||
} from "fixtures/get-test-server.ts" | ||
import { seedDatabase } from "lib/database/seed.ts" | ||
|
||
test("withSessionAuth middleware - successful auth", async (t) => { | ||
const { axios, db } = await getTestServer(t, { seed: false }) | ||
const seed_result = seedDatabase(db) | ||
|
||
const token = jwt.sign( | ||
{ | ||
user_id: seed_result.john_user_id, | ||
key: seed_result.john_user_key, | ||
}, | ||
"secret", | ||
) | ||
|
||
// Test successful auth with workspace | ||
const { status } = await axios.get("/workspaces/get", { | ||
headers: { | ||
Authorization: `Bearer ${token}`, | ||
"Seam-Workspace": seed_result.seed_workspace_1, | ||
}, | ||
}) | ||
t.is(status, 200) | ||
|
||
// Test missing workspace header | ||
const missingWorkspaceErr = await t.throwsAsync<SimpleAxiosError>( | ||
axios.get("/workspaces/get", { | ||
headers: { | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
}), | ||
) | ||
|
||
t.is(missingWorkspaceErr?.status, 400) | ||
t.is(missingWorkspaceErr?.response.error.type, "missing_workspace_id") | ||
|
||
// Test invalid token | ||
const invalidTokenErr = await t.throwsAsync<SimpleAxiosError>( | ||
axios.get("/workspaces/get", { | ||
headers: { | ||
Authorization: "Bearer invalid_token", | ||
"Seam-Workspace": seed_result.seed_workspace_1, | ||
}, | ||
}), | ||
) | ||
|
||
t.is(invalidTokenErr?.status, 500) | ||
|
||
// Test unauthorized workspace access | ||
const unauthorizedErr = await t.throwsAsync<SimpleAxiosError>( | ||
axios.get("/workspaces/get", { | ||
headers: { | ||
Authorization: `Bearer ${token}`, | ||
"Seam-Workspace": "invalid_workspace_id", | ||
}, | ||
}), | ||
) | ||
|
||
t.is(unauthorizedErr?.status, 401) | ||
t.is(unauthorizedErr?.response.error.type, "unauthorized") | ||
}) |