Skip to content

Commit e01d5f9

Browse files
Add test
1 parent b7d9e59 commit e01d5f9

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
import test, { type ExecutionContext } from "ava"
2+
3+
import {
4+
getTestServer,
5+
type SimpleAxiosError,
6+
} from "fixtures/get-test-server.ts"
7+
import { seedDatabase } from "lib/database/seed.ts"
8+
9+
test("GET /connected_accounts/list with limit", async (t: ExecutionContext) => {
10+
const { axios, db } = await getTestServer(t, { seed: false })
11+
const seed_result = seedDatabase(db)
12+
13+
axios.defaults.headers.common.Authorization = `Bearer ${seed_result.seam_apikey1_token}`
14+
15+
const {
16+
data: { connected_accounts },
17+
} = await axios.get("/connected_accounts/list", { params: { limit: 1 } })
18+
19+
t.is(connected_accounts.length, 1)
20+
})
21+
22+
test("GET /connected_accounts/list with pages", async (t: ExecutionContext) => {
23+
const { axios, db } = await getTestServer(t, { seed: false })
24+
const seed_result = seedDatabase(db)
25+
26+
const params = { limit: 2 }
27+
28+
axios.defaults.headers.common.Authorization = `Bearer ${seed_result.seam_apikey1_token}`
29+
30+
const {
31+
data: {
32+
connected_accounts,
33+
pagination: { has_next_page, next_page_cursor, next_page_url },
34+
},
35+
} = await axios.get("/connected_accounts/list")
36+
t.false(has_next_page)
37+
t.is(next_page_cursor, null)
38+
t.is(next_page_url, null)
39+
t.is(connected_accounts.length, 3)
40+
41+
const {
42+
data: {
43+
connected_accounts: page1,
44+
pagination: {
45+
has_next_page: has_page_2,
46+
next_page_cursor: page2_cursor,
47+
next_page_url: page2_url,
48+
},
49+
},
50+
} = await axios.get("/connected_accounts/list", { params })
51+
52+
t.is(page1.length, 2)
53+
t.true(has_page_2)
54+
t.truthy(page2_cursor)
55+
56+
if (page2_url == null) {
57+
t.fail("Null next_page_url")
58+
return
59+
}
60+
61+
const url = new URL(page2_url)
62+
t.is(url.pathname, "/connected_accounts/list")
63+
t.deepEqual(url.searchParams.getAll("limit"), ["2"])
64+
65+
t.deepEqual(page1, [connected_accounts[0], connected_accounts[1]])
66+
67+
const {
68+
data: {
69+
connected_accounts: page2,
70+
pagination: { has_next_page: has_page_3, next_page_cursor: page3_cursor },
71+
},
72+
} = await axios.get("/connected_accounts/list", {
73+
params: { ...params, page_cursor: page2_cursor },
74+
})
75+
t.is(page2.length, 1)
76+
t.false(has_page_3)
77+
t.is(page3_cursor, null)
78+
79+
t.deepEqual(page2, [connected_accounts[2]])
80+
})
81+
82+
test("GET /connected_accounts/list validates query hash", async (t: ExecutionContext) => {
83+
const { axios, db } = await getTestServer(t, { seed: false })
84+
const seed_result = seedDatabase(db)
85+
86+
axios.defaults.headers.common.Authorization = `Bearer ${seed_result.seam_apikey1_token}`
87+
88+
const {
89+
data: {
90+
pagination: { has_next_page, next_page_cursor },
91+
},
92+
} = await axios.get("/connected_accounts/list", { params: { limit: 2 } })
93+
94+
t.true(has_next_page)
95+
96+
const err = await t.throwsAsync<SimpleAxiosError>(
97+
async () =>
98+
await axios.get("/connected_accounts/list", {
99+
params: { limit: 3, page_cursor: next_page_cursor },
100+
}),
101+
)
102+
t.is(err?.status, 400)
103+
t.regex(
104+
(err?.response?.error?.message as string) ?? "",
105+
/parameters identical/,
106+
)
107+
108+
const err_empty = await t.throwsAsync<SimpleAxiosError>(
109+
async () =>
110+
await axios.get("/connected_accounts/list", {
111+
params: { page_cursor: next_page_cursor },
112+
}),
113+
)
114+
t.is(err_empty?.status, 400)
115+
t.regex(
116+
(err_empty?.response?.error?.message as string) ?? "",
117+
/parameters identical/,
118+
)
119+
120+
const err_post = await t.throwsAsync<SimpleAxiosError>(
121+
async () =>
122+
await axios.post("/connected_accounts/list", {
123+
limit: 3,
124+
page_cursor: next_page_cursor,
125+
}),
126+
)
127+
t.is(err_post?.status, 400)
128+
t.regex(
129+
(err_post?.response?.error?.message as string) ?? "",
130+
/parameters identical/,
131+
)
132+
})

0 commit comments

Comments
 (0)