-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcreate.test.js
More file actions
165 lines (146 loc) · 5.34 KB
/
Copy pathcreate.test.js
File metadata and controls
165 lines (146 loc) · 5.34 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
import express from "express"
import request from "supertest"
import { jest } from "@jest/globals"
import createRoute from "../create.js"
import { messenger } from "../../error-messenger.js"
//import app from "../../app.js"
const routeTester = new express()
routeTester.use(express.json())
routeTester.use(express.urlencoded({ extended: false }))
routeTester.use("/create", createRoute)
routeTester.use("/app/create", createRoute)
routeTester.use(messenger)
const rerum_uri = `${process.env.RERUM_ID_PATTERN}_not_`
beforeEach(() => {
/**
* Request/Response Mock Using manual fetch replacement
* This is overruling the fetch(store.rerum.io/v1/api/create) call in create.js
*/
global.fetch = jest.fn(() =>
Promise.resolve({
json: () => Promise.resolve({ "@id": rerum_uri, "test": "item", "__rerum": { "stuff": "here" } }),
ok: true,
text: () => Promise.resolve("Descriptive Error Here")
})
)
})
afterEach(() => {
/**
* Food for thought: delete data generated by tests?
* Make a test.store available that uses the same annotationStoreTesting as RERUM tests?
*/
})
/**
* This test suite runs the logic of the route file 'create.js' but does not actually communicate with RERUM.
* It will confirm the following:
* - Is the express req/resp sent into the route
* - Can the route read the JSON body
* - Does the route add @id and __rerum
* - Does the route respond 201
* - Does the route respond with the object that was in the request body
* - Does the route respond with the proper 'Location' header
*
* Note: /app/create uses the same logic and would be a redundant test.
*/
describe("Check that the request/response behavior of the TinyNode create route functions. Mock the connection to RERUM. __mock_functions", () => {
it("'/create' route request and response behavior is functioning.", async () => {
const response = await request(routeTester)
.post("/create")
.send({ "test": "item" })
.set("Content-Type", "application/json")
.then(resp => resp)
.catch(err => err)
expect(response.header.location).toBe(rerum_uri)
expect(response.statusCode).toBe(201)
expect(response.body.test).toBe("item")
})
})
/**
* This test suite checks the RESTful responses when using the TinyNode create endpoint incorrectly.
*
* - Incorrect HTTP method
* - Invalid JSON body
*
* Note: /app/create uses the same logic and would be a redundant test
*/
describe("Check that incorrect TinyNode create route usage results in expected RESTful responses from RERUM. __rest __core", () => {
it("Incorrect '/create' route usage has expected RESTful responses.", async () => {
let response = null
response = await request(routeTester)
.get("/create")
.then(resp => resp)
.catch(err => err)
expect(response.statusCode).toBe(405)
response = await request(routeTester)
.put("/create")
.then(resp => resp)
.catch(err => err)
expect(response.statusCode).toBe(405)
response = await request(routeTester)
.patch("/create")
.then(resp => resp)
.catch(err => err)
expect(response.statusCode).toBe(405)
response = await request(routeTester)
.delete("/create")
.then(resp => resp)
.catch(err => err)
expect(response.statusCode).toBe(405)
response = await request(routeTester)
.post("/create")
.set("Content-Type", "application/json")
.send("not json")
.then(resp => resp)
.catch(err => err)
expect(response.statusCode).toBe(400)
})
})
describe("Check that TinyNode create route propagates upstream and network errors predictably. __rest __core", () => {
it("Preserves upstream text errors and maps network failures to 502.", async () => {
global.fetch = jest.fn(() =>
Promise.resolve({
ok: false,
status: 503,
headers: {
get: () => "text/plain; charset=utf-8"
},
text: () => Promise.resolve("Upstream create failure")
})
)
let response = await request(routeTester)
.post("/create")
.set("Content-Type", "application/json")
.send({ "test": "item" })
.then(resp => resp)
.catch(err => err)
expect(response.statusCode).toBe(502)
expect(response.text).toContain("Upstream create failure")
global.fetch = jest.fn(() => Promise.reject(new Error("socket hang up")))
response = await request(routeTester)
.post("/create")
.set("Content-Type", "application/json")
.send({ "test": "item" })
.then(resp => resp)
.catch(err => err)
expect(response.statusCode).toBe(502)
expect(response.text).toContain("A RERUM error occurred")
})
})
/**
* Full integration test. Checks the TinyNode app create endpoint functionality and RERUM connection.
*
* Note: /app/create uses the same logic and would be a redundant test
*/
describe("Check that the properly used create endpoints function and interact with RERUM. __e2e", () => {
it("'/create' route can save an object to RERUM.", async () => {
const response = await request(routeTester)
.post("/create")
.send({ "test": "item" })
.set("Content-Type", "application/json")
.then(resp => resp)
.catch(err => err)
expect(response.header).toHaveProperty('location')
expect(response.statusCode).toBe(201)
expect(response.body.test).toBe("item")
})
})