@sera4/essentia 1.1.40 → 1.1.41
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/helpers/index.js +1 -0
- package/helpers/test-server-wrapper.js +167 -0
- package/index.js +1 -0
- package/package.json +1 -1
- package/package.tar.gz +0 -0
package/helpers/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./test-server-wrapper.js";
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import uuidv4 from 'uuid/v4.js';
|
|
2
|
+
import fs from "fs";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A small wrapper around an express js server
|
|
6
|
+
* meant to be used in unit tests
|
|
7
|
+
*/
|
|
8
|
+
export class TestServerWrapper {
|
|
9
|
+
|
|
10
|
+
constructor(apiPrefix, expressServer, defaultHeaders, oldAuthToken, routePrinter, chai) {
|
|
11
|
+
this.server = expressServer;
|
|
12
|
+
this.defaultHeaders = defaultHeaders;
|
|
13
|
+
this.apiPrefix = apiPrefix;
|
|
14
|
+
this.routePrinter = routePrinter;
|
|
15
|
+
this.chai = chai;
|
|
16
|
+
|
|
17
|
+
// Support for older/deprecated apis
|
|
18
|
+
const oldAuthHeaders = {field: "Authorization", value: `Token organization_token=${oldAuthToken}`};
|
|
19
|
+
this.v1 = {
|
|
20
|
+
get: (url) => this.getWithPrefix("/v1", url, oldAuthHeaders),
|
|
21
|
+
post: (url) => this.postWithPrefix("/v1", url, oldAuthHeaders),
|
|
22
|
+
put: (url) => this.putWithPrefix("/v1", url, oldAuthHeaders),
|
|
23
|
+
delete: (url) => this.deleteWithPrefix("/v1", url, oldAuthHeaders)
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Adds a leading slash to the url if missing
|
|
29
|
+
* @param {String} url the url to sanitize
|
|
30
|
+
* @returns sanitized url
|
|
31
|
+
*/
|
|
32
|
+
sanitizeUrl (url) {
|
|
33
|
+
// add leading slash if missing
|
|
34
|
+
if (!url.startsWith("/")) {
|
|
35
|
+
return "/" + url;
|
|
36
|
+
} else {
|
|
37
|
+
return url;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Apply given headers to the request object
|
|
43
|
+
* @param {Object} request
|
|
44
|
+
* @param {Object} headers
|
|
45
|
+
*/
|
|
46
|
+
applyHeaders (request, headers) {
|
|
47
|
+
Object.keys(headers).map(k => {
|
|
48
|
+
request.set(k, headers[k]);
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
get (url, options) {
|
|
53
|
+
return this.getWithPrefix(this.apiPrefix, url, options);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
getWithPrefix (prefix, url, options) {
|
|
57
|
+
const sanitizedUrl = this.sanitizeUrl(url);
|
|
58
|
+
const request = this.chai.request(this.server).get(`${prefix}${sanitizedUrl}`);
|
|
59
|
+
this.applyHeaders(request, this.defaultHeaders);
|
|
60
|
+
|
|
61
|
+
if (options?.field) {
|
|
62
|
+
this.applyHeaders(request, {[options.field] : options.value});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (options?.body) {
|
|
66
|
+
request.send(options.body);
|
|
67
|
+
}
|
|
68
|
+
return request;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
post (url, options) {
|
|
72
|
+
return this.postWithPrefix(this.apiPrefix, url, options);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
postWithPrefix (prefix, url, options) {
|
|
76
|
+
const sanitizedUrl = this.sanitizeUrl(url);
|
|
77
|
+
const request = this.chai.request(this.server).post(`${prefix}${sanitizedUrl}`);
|
|
78
|
+
this.applyHeaders(request, this.defaultHeaders);
|
|
79
|
+
|
|
80
|
+
if (options?.field) {
|
|
81
|
+
this.applyHeaders(request, {[options.field] : options.value});
|
|
82
|
+
}
|
|
83
|
+
return request;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
put (url, options) {
|
|
87
|
+
return this.putWithPrefix(this.apiPrefix, url, options);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
putWithPrefix (prefix, url, options) {
|
|
91
|
+
const sanitizedUrl = this.sanitizeUrl(url);
|
|
92
|
+
const request = this.chai.request(this.server).put(`${prefix}${sanitizedUrl}`);
|
|
93
|
+
this.applyHeaders(request, this.defaultHeaders);
|
|
94
|
+
|
|
95
|
+
if (options?.field) {
|
|
96
|
+
this.applyHeaders(request, {[options.field] : options.value});
|
|
97
|
+
}
|
|
98
|
+
return request;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
delete (url, options) {
|
|
102
|
+
return this.deleteWithPrefix(this.apiPrefix, url, options);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
deleteWithPrefix (prefix, url, options) {
|
|
106
|
+
const sanitizedUrl = this.sanitizeUrl(url);
|
|
107
|
+
const request = this.chai.request(this.server).delete(`${prefix}${sanitizedUrl}`);
|
|
108
|
+
this.applyHeaders(request, this.defaultHeaders);
|
|
109
|
+
|
|
110
|
+
if (options?.field) {
|
|
111
|
+
this.applyHeaders(request, {[options.field] : options.value});
|
|
112
|
+
}
|
|
113
|
+
return request;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
upload ( url, fields, file, options) {
|
|
117
|
+
return this.uploadWithPrefix(this.apiPrefix, url, fields, file, options);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
uploadWithPrefix (prefix, url, fields, file, options) {
|
|
121
|
+
const sanitizedUrl = this.sanitizeUrl(url);
|
|
122
|
+
const request = this.chai.request(this.server).post(`${prefix}${sanitizedUrl}`);
|
|
123
|
+
this.applyHeaders(request, this.defaultHeaders);
|
|
124
|
+
|
|
125
|
+
if (options?.field) {
|
|
126
|
+
this.applyHeaders(request, {[options.field] : options.value});
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
request.attach("data", fs.readFileSync(file), {filename: "data"});
|
|
130
|
+
const keys = Object.keys(fields);
|
|
131
|
+
keys.forEach(k => {
|
|
132
|
+
request.field(k, fields[k]);
|
|
133
|
+
});
|
|
134
|
+
return request;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
stop () {
|
|
138
|
+
this.server.close();
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
allRoutes () {
|
|
142
|
+
return this.routePrinter.getAll();
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// Support for injecting certain memberships in every request
|
|
146
|
+
withMembership (m) {
|
|
147
|
+
const headers = { field: "tws-membership", value: JSON.stringify(m) }
|
|
148
|
+
return {
|
|
149
|
+
get: (url, options) => this.get(url, {...headers, ...options}),
|
|
150
|
+
post: (url, options) => this.post(url, {...headers, ...options}),
|
|
151
|
+
put: (url, options) => this.put(url, {...headers, ...options}),
|
|
152
|
+
delete: (url, options) => this.delete(url, {...headers, ...options}),
|
|
153
|
+
upload: (url, fields, file, options) => this.upload(url, fields, file, {...headers, ...options}),
|
|
154
|
+
stop: this.stop,
|
|
155
|
+
allRoutes: this.allRoutes,
|
|
156
|
+
v1: this.v1
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
withS4 () {
|
|
161
|
+
return this.withMembership({s4_role: 1, id: uuidv4(), account_id: uuidv4(), tenant_id: uuidv4()});
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
withTap() {
|
|
165
|
+
return this.withMembership({s4_role: 2, id: uuidv4(), account_id: uuidv4(), tenant_id: uuidv4()});
|
|
166
|
+
}
|
|
167
|
+
}
|
package/index.js
CHANGED
package/package.json
CHANGED
package/package.tar.gz
CHANGED
|
Binary file
|