Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add /_fake/add_client_session #206

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions src/lib/database/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,10 @@ export interface DatabaseMethods {
long_token_hash: string
created_at?: string
}) => AccessToken
addClientSession: (params: {
workspace_id: WorkspaceId
connected_account_ids?: string[]
connect_webview_ids?: string[]
user_identifier_key: string
user_identity_ids?: string[]
token?: string
created_at?: string
}) => ClientSession
addClientSession: (
params: Partial<ClientSession> &
Pick<ClientSession, "workspace_id" | "user_identifier_key">,
) => ClientSession
addUserIdentity: (params: {
workspace_id: WorkspaceId
user_identity_id?: string
Expand Down
34 changes: 34 additions & 0 deletions src/pages/api/_fake/add_client_session.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { NotFoundException } from "nextlove"
import { z } from "zod"

import { withRouteSpec } from "lib/middleware/with-route-spec.ts"
import { client_session } from "lib/zod/client_session.ts"

export default withRouteSpec({
auth: "none",
methods: ["POST"],
jsonBody: client_session
.partial()
.required({ workspace_id: true, user_identifier_key: true }),
jsonResponse: z.object({ client_session }),
} as const)(async (req, res) => {
const { workspace_id, ...client_session_create_payload } = req.body

const workspace = req.db.workspaces.find(
(w) => w.workspace_id === workspace_id,
)

if (workspace == null) {
throw new NotFoundException({
type: "workspace_not_found",
message: "Workspace not found",
})
}

const client_session = req.db.addClientSession({
workspace_id,
...client_session_create_payload,
})

res.status(200).json({ client_session })
})
32 changes: 32 additions & 0 deletions src/route-types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,36 @@
export type Routes = {
"/_fake/add_client_session": {
route: "/_fake/add_client_session"
method: "POST"
queryParams: {}
jsonBody: {
client_session_id?: string | undefined
device_count?: (number | undefined) | undefined
workspace_id: string
token?: string | undefined
user_identifier_key: string
user_identity_ids?: string[] | undefined
connect_webview_ids?: string[] | undefined
connected_account_ids?: string[] | undefined
created_at?: string | undefined
}
commonParams: {}
formData: {}
jsonResponse: {
client_session: {
client_session_id: string
device_count?: number | undefined
workspace_id: string
token: string
user_identifier_key: string
user_identity_ids: string[]
connect_webview_ids: string[]
connected_account_ids: string[]
created_at: string
}
ok: boolean
}
}
"/_fake/complete_connect_webview": {
route: "/_fake/complete_connect_webview"
method: "POST"
Expand Down
43 changes: 43 additions & 0 deletions test/api/_fake/add_client_session.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import test, { type ExecutionContext } from "ava"

import {
getTestServer,
type SimpleAxiosError,
} from "fixtures/get-test-server.ts"

test("POST /_fake/add_client_session", async (t: ExecutionContext) => {
const { axios, seed, db } = await getTestServer(t)

const initialClientSessionsCount = db.client_sessions.length

const payload = {
workspace_id: seed.ws1.workspace_id,
user_identifier_key: "some-new-user-identifier-key",
}

const {
status,
data: { client_session },
} = await axios.post("/_fake/add_client_session", payload)

t.is(status, 200)
t.is(db.client_sessions.length, initialClientSessionsCount + 1)

t.is(client_session.workspace_id, payload.workspace_id)
t.is(client_session.user_identifier_key, payload.user_identifier_key)
})

test("POST /_fake/add_client_session - invalid workspace_id", async (t: ExecutionContext) => {
const { axios } = await getTestServer(t)

const payload = {
workspace_id: "non-existent-workspace-id",
user_identifier_key: "some-user-identifier-key",
}

const err = await t.throwsAsync<SimpleAxiosError>(
async () => await axios.post("/_fake/add_client_session", payload),
)

t.is(err?.status, 404)
})
Loading