aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--server/tests/api/check-params/contact-form.ts69
-rw-r--r--server/tests/api/server/contact-form.ts23
-rw-r--r--shared/extra-utils/server/contact-form-command.ts31
-rw-r--r--shared/extra-utils/server/contact-form.ts31
-rw-r--r--shared/extra-utils/server/index.ts1
-rw-r--r--shared/extra-utils/server/servers.ts3
6 files changed, 62 insertions, 96 deletions
diff --git a/server/tests/api/check-params/contact-form.ts b/server/tests/api/check-params/contact-form.ts
index 274562cbb..fb30766d9 100644
--- a/server/tests/api/check-params/contact-form.ts
+++ b/server/tests/api/check-params/contact-form.ts
@@ -1,10 +1,9 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2 2
3import 'mocha' 3import 'mocha'
4import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' 4import { HttpStatusCode } from '@shared/core-utils'
5import { cleanupTests, flushAndRunServer, immutableAssign, killallServers, reRunServer, ServerInfo } from '../../../../shared/extra-utils' 5import { cleanupTests, flushAndRunServer, killallServers, MockSmtpServer, reRunServer, ServerInfo } from '@shared/extra-utils'
6import { MockSmtpServer } from '../../../../shared/extra-utils/mock-servers/mock-email' 6import { ContactFormCommand } from '@shared/extra-utils/server'
7import { sendContactForm } from '../../../../shared/extra-utils/server/contact-form'
8 7
9describe('Test contact form API validators', function () { 8describe('Test contact form API validators', function () {
10 let server: ServerInfo 9 let server: ServerInfo
@@ -16,6 +15,7 @@ describe('Test contact form API validators', function () {
16 body: 'Hello, how are you?' 15 body: 'Hello, how are you?'
17 } 16 }
18 let emailPort: number 17 let emailPort: number
18 let command: ContactFormCommand
19 19
20 // --------------------------------------------------------------- 20 // ---------------------------------------------------------------
21 21
@@ -26,10 +26,11 @@ describe('Test contact form API validators', function () {
26 26
27 // Email is disabled 27 // Email is disabled
28 server = await flushAndRunServer(1) 28 server = await flushAndRunServer(1)
29 command = server.contactFormCommand
29 }) 30 })
30 31
31 it('Should not accept a contact form if emails are disabled', async function () { 32 it('Should not accept a contact form if emails are disabled', async function () {
32 await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: HttpStatusCode.CONFLICT_409 })) 33 await command.send({ ...defaultBody, expectedStatus: HttpStatusCode.CONFLICT_409 })
33 }) 34 })
34 35
35 it('Should not accept a contact form if it is disabled in the configuration', async function () { 36 it('Should not accept a contact form if it is disabled in the configuration', async function () {
@@ -39,7 +40,7 @@ describe('Test contact form API validators', function () {
39 40
40 // Contact form is disabled 41 // Contact form is disabled
41 await reRunServer(server, { smtp: { hostname: 'localhost', port: emailPort }, contact_form: { enabled: false } }) 42 await reRunServer(server, { smtp: { hostname: 'localhost', port: emailPort }, contact_form: { enabled: false } })
42 await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: HttpStatusCode.CONFLICT_409 })) 43 await command.send({ ...defaultBody, expectedStatus: HttpStatusCode.CONFLICT_409 })
43 }) 44 })
44 45
45 it('Should not accept a contact form if from email is invalid', async function () { 46 it('Should not accept a contact form if from email is invalid', async function () {
@@ -50,61 +51,25 @@ describe('Test contact form API validators', function () {
50 // Email & contact form enabled 51 // Email & contact form enabled
51 await reRunServer(server, { smtp: { hostname: 'localhost', port: emailPort } }) 52 await reRunServer(server, { smtp: { hostname: 'localhost', port: emailPort } })
52 53
53 await sendContactForm(immutableAssign(defaultBody, { 54 await command.send({ ...defaultBody, fromEmail: 'badEmail', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
54 url: server.url, 55 await command.send({ ...defaultBody, fromEmail: 'badEmail@', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
55 expectedStatus: HttpStatusCode.BAD_REQUEST_400, 56 await command.send({ ...defaultBody, fromEmail: undefined, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
56 fromEmail: 'badEmail'
57 }))
58 await sendContactForm(immutableAssign(defaultBody, {
59 url: server.url,
60 expectedStatus: HttpStatusCode.BAD_REQUEST_400,
61 fromEmail: 'badEmail@'
62 }))
63 await sendContactForm(immutableAssign(defaultBody, {
64 url: server.url,
65 expectedStatus: HttpStatusCode.BAD_REQUEST_400,
66 fromEmail: undefined
67 }))
68 }) 57 })
69 58
70 it('Should not accept a contact form if from name is invalid', async function () { 59 it('Should not accept a contact form if from name is invalid', async function () {
71 await sendContactForm(immutableAssign(defaultBody, { 60 await command.send({ ...defaultBody, fromName: 'name'.repeat(100), expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
72 url: server.url, 61 await command.send({ ...defaultBody, fromName: '', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
73 expectedStatus: HttpStatusCode.BAD_REQUEST_400, 62 await command.send({ ...defaultBody, fromName: undefined, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
74 fromName: 'name'.repeat(100)
75 }))
76 await sendContactForm(immutableAssign(defaultBody, {
77 url: server.url,
78 expectedStatus: HttpStatusCode.BAD_REQUEST_400,
79 fromName: ''
80 }))
81 await sendContactForm(immutableAssign(defaultBody, {
82 url: server.url,
83 expectedStatus: HttpStatusCode.BAD_REQUEST_400,
84 fromName: undefined
85 }))
86 }) 63 })
87 64
88 it('Should not accept a contact form if body is invalid', async function () { 65 it('Should not accept a contact form if body is invalid', async function () {
89 await sendContactForm(immutableAssign(defaultBody, { 66 await command.send({ ...defaultBody, body: 'body'.repeat(5000), expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
90 url: server.url, 67 await command.send({ ...defaultBody, body: 'a', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
91 expectedStatus: HttpStatusCode.BAD_REQUEST_400, 68 await command.send({ ...defaultBody, body: undefined, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
92 body: 'body'.repeat(5000)
93 }))
94 await sendContactForm(immutableAssign(defaultBody, {
95 url: server.url,
96 expectedStatus: HttpStatusCode.BAD_REQUEST_400,
97 body: 'a'
98 }))
99 await sendContactForm(immutableAssign(defaultBody, {
100 url: server.url,
101 expectedStatus: HttpStatusCode.BAD_REQUEST_400,
102 body: undefined
103 }))
104 }) 69 })
105 70
106 it('Should accept a contact form with the correct parameters', async function () { 71 it('Should accept a contact form with the correct parameters', async function () {
107 await sendContactForm(immutableAssign(defaultBody, { url: server.url })) 72 await command.send({ ...defaultBody })
108 }) 73 })
109 74
110 after(async function () { 75 after(async function () {
diff --git a/server/tests/api/server/contact-form.ts b/server/tests/api/server/contact-form.ts
index 71205723d..79c4c6748 100644
--- a/server/tests/api/server/contact-form.ts
+++ b/server/tests/api/server/contact-form.ts
@@ -2,17 +2,16 @@
2 2
3import 'mocha' 3import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' 5import { HttpStatusCode } from '@shared/core-utils'
6import { cleanupTests, flushAndRunServer, ServerInfo, setAccessTokensToServers, wait } from '../../../../shared/extra-utils' 6import { cleanupTests, flushAndRunServer, MockSmtpServer, ServerInfo, setAccessTokensToServers, wait, waitJobs } from '@shared/extra-utils'
7import { MockSmtpServer } from '../../../../shared/extra-utils/mock-servers/mock-email' 7import { ContactFormCommand } from '@shared/extra-utils/server'
8import { sendContactForm } from '../../../../shared/extra-utils/server/contact-form'
9import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
10 8
11const expect = chai.expect 9const expect = chai.expect
12 10
13describe('Test contact form', function () { 11describe('Test contact form', function () {
14 let server: ServerInfo 12 let server: ServerInfo
15 const emails: object[] = [] 13 const emails: object[] = []
14 let command: ContactFormCommand
16 15
17 before(async function () { 16 before(async function () {
18 this.timeout(30000) 17 this.timeout(30000)
@@ -27,13 +26,14 @@ describe('Test contact form', function () {
27 } 26 }
28 server = await flushAndRunServer(1, overrideConfig) 27 server = await flushAndRunServer(1, overrideConfig)
29 await setAccessTokensToServers([ server ]) 28 await setAccessTokensToServers([ server ])
29
30 command = server.contactFormCommand
30 }) 31 })
31 32
32 it('Should send a contact form', async function () { 33 it('Should send a contact form', async function () {
33 this.timeout(10000) 34 this.timeout(10000)
34 35
35 await sendContactForm({ 36 await command.send({
36 url: server.url,
37 fromEmail: 'toto@example.com', 37 fromEmail: 'toto@example.com',
38 body: 'my super message', 38 body: 'my super message',
39 subject: 'my subject', 39 subject: 'my subject',
@@ -58,16 +58,14 @@ describe('Test contact form', function () {
58 58
59 await wait(1000) 59 await wait(1000)
60 60
61 await sendContactForm({ 61 await command.send({
62 url: server.url,
63 fromEmail: 'toto@example.com', 62 fromEmail: 'toto@example.com',
64 body: 'my super message', 63 body: 'my super message',
65 subject: 'my subject', 64 subject: 'my subject',
66 fromName: 'Super toto' 65 fromName: 'Super toto'
67 }) 66 })
68 67
69 await sendContactForm({ 68 await command.send({
70 url: server.url,
71 fromEmail: 'toto@example.com', 69 fromEmail: 'toto@example.com',
72 body: 'my super message', 70 body: 'my super message',
73 fromName: 'Super toto', 71 fromName: 'Super toto',
@@ -79,8 +77,7 @@ describe('Test contact form', function () {
79 it('Should be able to send another contact form after a while', async function () { 77 it('Should be able to send another contact form after a while', async function () {
80 await wait(1000) 78 await wait(1000)
81 79
82 await sendContactForm({ 80 await command.send({
83 url: server.url,
84 fromEmail: 'toto@example.com', 81 fromEmail: 'toto@example.com',
85 fromName: 'Super toto', 82 fromName: 'Super toto',
86 subject: 'my subject', 83 subject: 'my subject',
diff --git a/shared/extra-utils/server/contact-form-command.ts b/shared/extra-utils/server/contact-form-command.ts
new file mode 100644
index 000000000..943e5ccbb
--- /dev/null
+++ b/shared/extra-utils/server/contact-form-command.ts
@@ -0,0 +1,31 @@
1import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes'
2import { ContactForm } from '../../models/server'
3import { AbstractCommand, OverrideCommandOptions } from '../shared'
4
5export class ContactFormCommand extends AbstractCommand {
6
7 send (options: OverrideCommandOptions & {
8 fromEmail: string
9 fromName: string
10 subject: string
11 body: string
12 }) {
13 const path = '/api/v1/server/contact'
14
15 const body: ContactForm = {
16 fromEmail: options.fromEmail,
17 fromName: options.fromName,
18 subject: options.subject,
19 body: options.body
20 }
21
22 return this.postBodyRequest({
23 ...options,
24
25 path,
26 token: null,
27 fields: body,
28 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
29 })
30 }
31}
diff --git a/shared/extra-utils/server/contact-form.ts b/shared/extra-utils/server/contact-form.ts
deleted file mode 100644
index 6c9232cc6..000000000
--- a/shared/extra-utils/server/contact-form.ts
+++ /dev/null
@@ -1,31 +0,0 @@
1import * as request from 'supertest'
2import { ContactForm } from '../../models/server'
3import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
4
5function sendContactForm (options: {
6 url: string
7 fromEmail: string
8 fromName: string
9 subject: string
10 body: string
11 expectedStatus?: number
12}) {
13 const path = '/api/v1/server/contact'
14
15 const body: ContactForm = {
16 fromEmail: options.fromEmail,
17 fromName: options.fromName,
18 subject: options.subject,
19 body: options.body
20 }
21 return request(options.url)
22 .post(path)
23 .send(body)
24 .expect(options.expectedStatus || HttpStatusCode.NO_CONTENT_204)
25}
26
27// ---------------------------------------------------------------------------
28
29export {
30 sendContactForm
31}
diff --git a/shared/extra-utils/server/index.ts b/shared/extra-utils/server/index.ts
new file mode 100644
index 000000000..4121c8828
--- /dev/null
+++ b/shared/extra-utils/server/index.ts
@@ -0,0 +1 @@
export * from './contact-form-command'
diff --git a/shared/extra-utils/server/servers.ts b/shared/extra-utils/server/servers.ts
index 8ccf790fc..b58639ba6 100644
--- a/shared/extra-utils/server/servers.ts
+++ b/shared/extra-utils/server/servers.ts
@@ -16,6 +16,7 @@ import { AbusesCommand } from '../moderation'
16import { OverviewsCommand } from '../overviews' 16import { OverviewsCommand } from '../overviews'
17import { makeGetRequest } from '../requests/requests' 17import { makeGetRequest } from '../requests/requests'
18import { SearchCommand } from '../search' 18import { SearchCommand } from '../search'
19import { ContactFormCommand } from './contact-form-command'
19 20
20interface ServerInfo { 21interface ServerInfo {
21 app: ChildProcess 22 app: ChildProcess
@@ -77,6 +78,7 @@ interface ServerInfo {
77 abusesCommand?: AbusesCommand 78 abusesCommand?: AbusesCommand
78 overviewsCommand?: OverviewsCommand 79 overviewsCommand?: OverviewsCommand
79 searchCommand?: SearchCommand 80 searchCommand?: SearchCommand
81 contactFormCommand?: ContactFormCommand
80} 82}
81 83
82function parallelTests () { 84function parallelTests () {
@@ -290,6 +292,7 @@ async function runServer (server: ServerInfo, configOverrideArg?: any, args = []
290 server.abusesCommand = new AbusesCommand(server) 292 server.abusesCommand = new AbusesCommand(server)
291 server.overviewsCommand = new OverviewsCommand(server) 293 server.overviewsCommand = new OverviewsCommand(server)
292 server.searchCommand = new SearchCommand(server) 294 server.searchCommand = new SearchCommand(server)
295 server.contactFormCommand = new ContactFormCommand(server)
293 296
294 res(server) 297 res(server)
295 }) 298 })