-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunit.test.js
More file actions
181 lines (156 loc) · 5.17 KB
/
unit.test.js
File metadata and controls
181 lines (156 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import {expect, jest} from "@jest/globals"
import request from "supertest"
import express from "express"
import User from "../User.js"
import privateProfileRouter from "../../../userProfile/privateProfile.js"
import mainApp from "../../../app.js"
jest.mock("../User.js")
jest.mock("../../../auth/index.js")
const app = express()
let token = process.env.TEST_TOKEN
// app.use( (req, res, next)=>{
// req.auth = {
// payload: {
// "http://store.rerum.io/agent": "agent_value",
// _id: "123456"
// },
// token:`Bearer ${token}`
// }
// req.user = {
// _id: "123456",
// username: "exampleUser",
// profile: { name: "John Doe" }
// }
// next()
// })
app.use("/my", privateProfileRouter)
describe.skip("GET /my/profile #user_class", () => {
it("should return 200", async () => {
const response = await request(mainApp)
.get("/my/profile")
.set("Authorization", `Bearer ${token}`)
expect(response.status).toBe(200)
})
})
describe.skip("GET /my/projects #user_class", () => {
it("should return 200", async () => {
const response = await request(mainApp)
.get("/my/projects")
.set("Authorization", `Bearer ${token}`)
expect(response.status).toBe(200)
})
})
describe.skip("GET /my/profile #user_class", () => {
it("should return 200", async () => {
const response = await request(app)
.get("/my/profile")
.set("Authorization", `Bearer ${token}`)
expect(response.status).toBe(200)
})
})
describe.skip("GET /my/projects #user_class", () => {
it("should return 200", async () => {
const response = await request(app)
.get("/my/projects")
.set("Authorization", `Bearer ${token}`)
expect(response.status).toBe(200)
})
})
describe("GET /my/profile #user_class", () => {
beforeAll(() => {
jest.spyOn(User.prototype, "getSelf").mockResolvedValue({
_id: "123456",
userData: {name: "VOO"}
})
})
afterAll(() => {
jest.restoreAllMocks()
})
it.skip("should return user profile data", async () => {
const response = await request(app)
.get("/my/profile")
.set("Authorization", `Bearer ${token}`)
expect(response.status).toBe(200)
expect(response.body).toHaveProperty("userData")
expect(response.body.userData).toEqual({name: "VOO"})
})
it("should return 401 if user is not authenticated (no authorization header)", async () => {
const appWithoutAuth = express()
appWithoutAuth.use("/my", privateProfileRouter)
const response = await request(appWithoutAuth).get("/my/profile")
expect(response.status).toBe(401)
})
it("should return 401 if user is not authenticated (invalid authorization header)", async () => {
const appWithoutAuth = express()
appWithoutAuth.use("/my", privateProfileRouter)
const response = await request(appWithoutAuth)
.get("/my/profile")
.set("Authorization", `Bearer 123123123123123123123123123`)
expect(response.status).toBe(401)
})
})
describe("GET /my/projects #user_class", () => {
beforeAll(() => {
jest.spyOn(User.prototype, "getProjects").mockResolvedValue([
{
_id: "project.id",
"@type": "Project",
creator: "user.agent",
groups: {
members: [{agent: "user.agent", _id: "user._id"}]
}
}
])
})
afterAll(() => {
jest.restoreAllMocks()
})
it.skip("should return user projects as an array", async () => {
const response = await request(app)
.get("/my/projects")
.set("Authorization", `Bearer ${token}`)
expect(response.status).toBe(200)
expect(Array.isArray(response.body)).toBe(true)
if (Array.isArray(response.body)) {
for (const obj of response.body) {
expect(obj).toHaveProperty("@type", "Project")
}
}
})
it("should return 401 if user is not authenticated (no authorization header)", async () => {
const appWithoutAuth = express()
appWithoutAuth.use("/my", privateProfileRouter)
const response = await request(appWithoutAuth).get("/my/profile")
expect(response.status).toBe(401)
})
it("should return 401 if user is not authenticated (invalid authorization header)", async () => {
const appWithoutAuth = express()
appWithoutAuth.use("/my", privateProfileRouter)
const response = await request(appWithoutAuth)
.get("/my/profile")
.set("Authorization", `Bearer 123123123123123123123123123`)
expect(response.status).toBe(401)
})
})
describe("GET /my/projects with no projects #user_class", () => {
beforeAll(() => {
jest.spyOn(User.prototype, "getProjects").mockResolvedValue([])
jest.spyOn(User.prototype, "getSelf").mockResolvedValue({
_id: "123456",
_lastModified: null
})
})
afterAll(() => {
jest.restoreAllMocks()
})
it.skip("should return 200 with empty projects array when user has no projects", async () => {
const response = await request(app)
.get("/my/projects")
.set("Authorization", `Bearer ${token}`)
expect(response.status).toBe(200)
expect(response.body).toHaveProperty("projects")
expect(Array.isArray(response.body.projects)).toBe(true)
expect(response.body.projects.length).toBe(0)
expect(response.body.metrics).toBeNull()
})
})