Skip to content

Commit a2bdba0

Browse files
committed
User auth and more tests
1 parent 070510d commit a2bdba0

3 files changed

Lines changed: 86 additions & 9 deletions

File tree

load_testing/auth.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { SharedArray } from "k6/data"
2+
3+
export function getAccessToken(): String {
4+
return __ENV.AUTH_ACCESS_TOKEN
5+
}
6+
7+
export const users = new SharedArray("users", function () {
8+
if (!__ENV.USERS_JSON_FILE) {
9+
return []
10+
}
11+
12+
return JSON.parse(open(__ENV.USERS_JSON_FILE))
13+
})
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Params } from "k6/http"
2+
import { Config } from "../../config.ts"
3+
import { MITLearnAPIClient as V0Client } from "./v0/api.ts"
4+
import { MITLearnAPIClient as V1Client } from "./v1/api.ts"
5+
import { getAccessToken } from "../../auth.ts"
6+
7+
function getCommonRequestParameters(): Params {
8+
return {
9+
headers: {
10+
Authorization: `Bearer ${getAccessToken()}`,
11+
},
12+
}
13+
}
14+
15+
export function createV0Client(config: Config): V0Client {
16+
return new V0Client({
17+
baseUrl: config.apiBaseUrl,
18+
commonRequestParameters: getCommonRequestParameters(),
19+
})
20+
}
21+
export function createV1Client(config: Config): V1Client {
22+
return new V1Client({
23+
baseUrl: config.apiBaseUrl,
24+
commonRequestParameters: getCommonRequestParameters(),
25+
})
26+
}

load_testing/backend/test.ts

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,54 @@
1+
import { check, group } from "k6"
2+
import exec from "k6/execution"
3+
import {
4+
randomItem,
5+
randomIntBetween,
6+
} from "https://jslib.k6.io/k6-utils/1.2.0/index.js"
7+
18
import { Config } from "../config.ts"
2-
import { check } from "k6"
3-
import { MITLearnAPIClient } from "./client/v0/api.ts"
9+
import { createV0Client, createV1Client } from "./client/client.ts"
410

511
export function configure(config: Config) {
6-
const v0Client = new MITLearnAPIClient({
7-
baseUrl: config.apiBaseUrl,
8-
})
912
return function () {
10-
const res = v0Client.videoShortsList({ limit: 50 })
11-
check(res, {
12-
"is status 200": (r) => r.status === 200,
13-
"has results": (r) => r.json("results").length > 0,
13+
group("api", function () {
14+
group("v0", function () {
15+
exec.vu.tags.apiVersion = "v0"
16+
17+
const client = createV0Client(config)
18+
19+
check(client.videoShortsList({ limit: 50 }), {
20+
"is status 200": (r) => r.response.status === 200,
21+
"has results": (r) => r.response.json("results").length > 0,
22+
})
23+
24+
delete exec.vu.tags.apiVersion
25+
})
26+
27+
group("v1", function () {
28+
exec.vu.tags.apiVersion = "v1"
29+
30+
const client = createV1Client(config)
31+
32+
group("learning resources", function () {
33+
const res = client.learningResourcesList({ limit: 50 })
34+
const results = res.response.json("results")
35+
check(res, {
36+
"is status 200": (r) => r.response.status === 200,
37+
"has results": (_) => results.length > 0,
38+
})
39+
40+
for (let index = 0; index < randomIntBetween(3, 5); index++) {
41+
const resource = randomItem(results)
42+
check(client.learningResourcesRetrieve(resource.id), {
43+
"is status 200": (r) => r.response.status === 200,
44+
"matches resource id": (r) =>
45+
r.response.json("id") == resource.id,
46+
})
47+
}
48+
})
49+
50+
delete exec.vu.tags.apiVersion
51+
})
1452
})
1553
}
1654
}

0 commit comments

Comments
 (0)