Skip to content

Commit e5e73a7

Browse files
committed
changed redirect response to provide headers instead of query parameters
1 parent 7a24c71 commit e5e73a7

File tree

2 files changed

+158
-141
lines changed

2 files changed

+158
-141
lines changed

spec/PublicAPI.spec.js

Lines changed: 94 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -3,105 +3,107 @@
33
const request = require('../lib/request');
44
const fs = require('fs').promises;
55
const Utils = require('../lib/Utils');
6-
const { PublicAPIRouter, pages } = require('../lib/Routers/PublicAPIRouter');
6+
const { PublicAPIRouter, pages, pageParams } = require('../lib/Routers/PublicAPIRouter');
77

88
describe('public API', () => {
9-
it('should return missing username error on ajax request without username provided', async () => {
10-
await reconfigureServer({
11-
publicServerURL: 'http://localhost:8378/1',
12-
});
13-
14-
try {
15-
await request({
16-
method: 'POST',
17-
url: 'http://localhost:8378/1/apps/test/request_password_reset',
18-
body: `new_password=user1&token=43634643&username=`,
19-
headers: {
20-
'Content-Type': 'application/x-www-form-urlencoded',
21-
'X-Requested-With': 'XMLHttpRequest',
22-
},
23-
followRedirects: false,
9+
describe('basic request', () => {
10+
it('should return missing username error on ajax request without username provided', async () => {
11+
await reconfigureServer({
12+
publicServerURL: 'http://localhost:8378/1',
2413
});
25-
} catch (error) {
26-
expect(error.status).not.toBe(302);
27-
expect(error.text).toEqual('{"code":200,"error":"Missing username"}');
28-
}
29-
});
3014

31-
it('should return missing token error on ajax request without token provided', async () => {
32-
await reconfigureServer({
33-
publicServerURL: 'http://localhost:8378/1',
15+
try {
16+
await request({
17+
method: 'POST',
18+
url: 'http://localhost:8378/1/apps/test/request_password_reset',
19+
body: `new_password=user1&token=43634643&username=`,
20+
headers: {
21+
'Content-Type': 'application/x-www-form-urlencoded',
22+
'X-Requested-With': 'XMLHttpRequest',
23+
},
24+
followRedirects: false,
25+
});
26+
} catch (error) {
27+
expect(error.status).not.toBe(302);
28+
expect(error.text).toEqual('{"code":200,"error":"Missing username"}');
29+
}
3430
});
3531

36-
try {
37-
await request({
38-
method: 'POST',
39-
url: 'http://localhost:8378/1/apps/test/request_password_reset',
40-
body: `new_password=user1&token=&username=Johnny`,
41-
headers: {
42-
'Content-Type': 'application/x-www-form-urlencoded',
43-
'X-Requested-With': 'XMLHttpRequest',
44-
},
45-
followRedirects: false,
32+
it('should return missing token error on ajax request without token provided', async () => {
33+
await reconfigureServer({
34+
publicServerURL: 'http://localhost:8378/1',
4635
});
47-
} catch (error) {
48-
expect(error.status).not.toBe(302);
49-
expect(error.text).toEqual('{"code":-1,"error":"Missing token"}');
50-
}
51-
});
5236

53-
it('should return missing password error on ajax request without password provided', async () => {
54-
await reconfigureServer({
55-
publicServerURL: 'http://localhost:8378/1',
37+
try {
38+
await request({
39+
method: 'POST',
40+
url: 'http://localhost:8378/1/apps/test/request_password_reset',
41+
body: `new_password=user1&token=&username=Johnny`,
42+
headers: {
43+
'Content-Type': 'application/x-www-form-urlencoded',
44+
'X-Requested-With': 'XMLHttpRequest',
45+
},
46+
followRedirects: false,
47+
});
48+
} catch (error) {
49+
expect(error.status).not.toBe(302);
50+
expect(error.text).toEqual('{"code":-1,"error":"Missing token"}');
51+
}
5652
});
5753

58-
try {
59-
await request({
60-
method: 'POST',
61-
url: 'http://localhost:8378/1/apps/test/request_password_reset',
62-
body: `new_password=&token=132414&username=Johnny`,
63-
headers: {
64-
'Content-Type': 'application/x-www-form-urlencoded',
65-
'X-Requested-With': 'XMLHttpRequest',
66-
},
67-
followRedirects: false,
54+
it('should return missing password error on ajax request without password provided', async () => {
55+
await reconfigureServer({
56+
publicServerURL: 'http://localhost:8378/1',
6857
});
69-
} catch (error) {
70-
expect(error.status).not.toBe(302);
71-
expect(error.text).toEqual('{"code":201,"error":"Missing password"}');
72-
}
73-
});
7458

75-
it('should get invalid_link.html', async () => {
76-
const httpResponse = await request({
77-
url: 'http://localhost:8378/1/apps/invalid_link.html',
59+
try {
60+
await request({
61+
method: 'POST',
62+
url: 'http://localhost:8378/1/apps/test/request_password_reset',
63+
body: `new_password=&token=132414&username=Johnny`,
64+
headers: {
65+
'Content-Type': 'application/x-www-form-urlencoded',
66+
'X-Requested-With': 'XMLHttpRequest',
67+
},
68+
followRedirects: false,
69+
});
70+
} catch (error) {
71+
expect(error.status).not.toBe(302);
72+
expect(error.text).toEqual('{"code":201,"error":"Missing password"}');
73+
}
7874
});
79-
expect(httpResponse.status).toBe(200);
80-
});
8175

82-
it('should get choose_password', async () => {
83-
await reconfigureServer({
84-
appName: 'unused',
85-
publicServerURL: 'http://localhost:8378/1',
76+
it('should get invalid_link.html', async () => {
77+
const httpResponse = await request({
78+
url: 'http://localhost:8378/1/apps/invalid_link.html',
79+
});
80+
expect(httpResponse.status).toBe(200);
8681
});
87-
const httpResponse = await request({
88-
url: 'http://localhost:8378/1/apps/choose_password?appId=test',
82+
83+
it('should get choose_password', async () => {
84+
await reconfigureServer({
85+
appName: 'unused',
86+
publicServerURL: 'http://localhost:8378/1',
87+
});
88+
const httpResponse = await request({
89+
url: 'http://localhost:8378/1/apps/choose_password?appId=test',
90+
});
91+
expect(httpResponse.status).toBe(200);
8992
});
90-
expect(httpResponse.status).toBe(200);
91-
});
9293

93-
it('should get verify_email_success.html', async () => {
94-
const httpResponse = await request({
95-
url: 'http://localhost:8378/1/apps/verify_email_success.html',
94+
it('should get verify_email_success.html', async () => {
95+
const httpResponse = await request({
96+
url: 'http://localhost:8378/1/apps/verify_email_success.html',
97+
});
98+
expect(httpResponse.status).toBe(200);
9699
});
97-
expect(httpResponse.status).toBe(200);
98-
});
99100

100-
it('should get password_reset_success.html', async () => {
101-
const httpResponse = await request({
102-
url: 'http://localhost:8378/1/apps/password_reset_success.html',
101+
it('should get password_reset_success.html', async () => {
102+
const httpResponse = await request({
103+
url: 'http://localhost:8378/1/apps/password_reset_success.html',
104+
});
105+
expect(httpResponse.status).toBe(200);
103106
});
104-
expect(httpResponse.status).toBe(200);
105107
});
106108

107109
describe('public API without publicServerURL', function () {
@@ -196,8 +198,8 @@ describe('public API', () => {
196198
beforeEach(async () => {
197199
router = new PublicAPIRouter();
198200
readFile = spyOn(fs, 'readFile').and.callThrough();
199-
pageResponse = spyOn(router, 'pageResponse').and.callThrough();
200-
redirectResponse = spyOn(router, 'redirectResponse').and.callThrough();
201+
pageResponse = spyOn(PublicAPIRouter.prototype, 'pageResponse').and.callThrough()
202+
redirectResponse = spyOn(PublicAPIRouter.prototype, 'redirectResponse').and.callThrough();
201203
req = {
202204
method: 'GET',
203205
config: {
@@ -247,8 +249,8 @@ describe('public API', () => {
247249
delete req.config.enablePageLocalization;
248250

249251
await expectAsync(router.goToPage(req, pages.invalidLink)).toBeResolved();
250-
expect(pageResponse.calls.all()[0].args[1]).toBeDefined();
251-
expect(pageResponse.calls.all()[0].args[1]).not.toMatch(
252+
expect(pageResponse.calls.all()[0].args[0]).toBeDefined();
253+
expect(pageResponse.calls.all()[0].args[0]).not.toMatch(
252254
new RegExp(`\/de(-AT)?\/${pages.invalidLink.defaultFile}`)
253255
);
254256
});
@@ -257,8 +259,8 @@ describe('public API', () => {
257259
delete req.query.locale;
258260

259261
await expectAsync(router.goToPage(req, pages.invalidLink)).toBeResolved();
260-
expect(pageResponse.calls.all()[0].args[1]).toBeDefined();
261-
expect(pageResponse.calls.all()[0].args[1]).not.toMatch(
262+
expect(pageResponse.calls.all()[0].args[0]).toBeDefined();
263+
expect(pageResponse.calls.all()[0].args[0]).not.toMatch(
262264
new RegExp(`\/de(-AT)?\/${pages.invalidLink.defaultFile}`)
263265
);
264266
});
@@ -273,8 +275,8 @@ describe('public API', () => {
273275

274276
it('returns file for locale match', async () => {
275277
await expectAsync(router.goToPage(req, pages.invalidLink)).toBeResolved();
276-
expect(pageResponse.calls.all()[0].args[1]).toBeDefined();
277-
expect(pageResponse.calls.all()[0].args[1]).toMatch(
278+
expect(pageResponse.calls.all()[0].args[0]).toBeDefined();
279+
expect(pageResponse.calls.all()[0].args[0]).toMatch(
278280
new RegExp(`\/de-AT\/${pages.invalidLink.defaultFile}`)
279281
);
280282
});
@@ -286,8 +288,8 @@ describe('public API', () => {
286288
});
287289

288290
await expectAsync(router.goToPage(req, pages.invalidLink)).toBeResolved();
289-
expect(pageResponse.calls.all()[0].args[1]).toBeDefined();
290-
expect(pageResponse.calls.all()[0].args[1]).toMatch(
291+
expect(pageResponse.calls.all()[0].args[0]).toBeDefined();
292+
expect(pageResponse.calls.all()[0].args[0]).toMatch(
291293
new RegExp(`\/de\/${pages.invalidLink.defaultFile}`)
292294
);
293295
});
@@ -296,8 +298,8 @@ describe('public API', () => {
296298
req.query.locale = 'yo-LO';
297299

298300
await expectAsync(router.goToPage(req, pages.invalidLink)).toBeResolved();
299-
expect(pageResponse.calls.all()[0].args[1]).toBeDefined();
300-
expect(pageResponse.calls.all()[0].args[1]).not.toMatch(
301+
expect(pageResponse.calls.all()[0].args[0]).toBeDefined();
302+
expect(pageResponse.calls.all()[0].args[0]).not.toMatch(
301303
new RegExp(`\/yo(-LO)?\/${pages.invalidLink.defaultFile}`)
302304
);
303305
});
@@ -315,7 +317,7 @@ describe('public API', () => {
315317
expect(redirectResponse).toHaveBeenCalled();
316318
});
317319

318-
it('returns a redirect for custom pages for GET and POST', async () => {
320+
it('returns a redirect for custom pages for GET and POST request', async () => {
319321
req.config.customPages = { invalidLink: 'http://invalid-link.example.com' };
320322

321323
for (const method of ['GET', 'POST']) {

0 commit comments

Comments
 (0)