diff options
author | 2024-02-10 18:19:14 -0700 | |
---|---|---|
committer | 2024-02-13 06:59:44 +0530 | |
commit | 7584d2d7a7110aef0331ebfa178b2295842c59fa (patch) | |
tree | 900cd71237e6231b57936fcce77ff229cd459041 /tests/functional/dashboard/transfer.spec.ts | |
parent | upgrade recipes submodule (diff) | |
download | ferdium-server-7584d2d7a7110aef0331ebfa178b2295842c59fa.tar.gz ferdium-server-7584d2d7a7110aef0331ebfa178b2295842c59fa.tar.zst ferdium-server-7584d2d7a7110aef0331ebfa178b2295842c59fa.zip |
refactor: project maintenance
- work in progress
Diffstat (limited to 'tests/functional/dashboard/transfer.spec.ts')
-rw-r--r-- | tests/functional/dashboard/transfer.spec.ts | 200 |
1 files changed, 75 insertions, 125 deletions
diff --git a/tests/functional/dashboard/transfer.spec.ts b/tests/functional/dashboard/transfer.spec.ts index e40fca2..41a6709 100644 --- a/tests/functional/dashboard/transfer.spec.ts +++ b/tests/functional/dashboard/transfer.spec.ts | |||
@@ -1,80 +1,59 @@ | |||
1 | import { test } from '@japa/runner'; | 1 | import { test } from '@japa/runner' |
2 | import { readFileSync } from 'node:fs'; | 2 | import { readFileSync } from 'node:fs' |
3 | import UserFactory from 'Database/factories/UserFactory'; | 3 | import UserFactory from '#database/factories/UserFactory' |
4 | 4 | ||
5 | test.group('Dashboard / Transfer page', () => { | 5 | test.group('Dashboard / Transfer page', () => { |
6 | test('redirects to /user/login when accessing /user/transfer as guest', async ({ | 6 | test('redirects to /user/login when accessing /user/transfer as guest', async ({ client }) => { |
7 | client, | 7 | const response = await client.get('/user/transfer') |
8 | }) => { | ||
9 | const response = await client.get('/user/transfer'); | ||
10 | 8 | ||
11 | response.assertRedirectsTo('/user/login'); // Check if it redirects to the expected URL | 9 | response.assertRedirectsTo('/user/login') // Check if it redirects to the expected URL |
12 | }); | 10 | }) |
13 | 11 | ||
14 | test('returns a 200 opening the transfer route while being logged in', async ({ | 12 | test('returns a 200 opening the transfer route while being logged in', async ({ client }) => { |
15 | client, | 13 | const user = await UserFactory.create() |
16 | }) => { | 14 | const response = await client.get('/user/transfer').loginAs(user) |
17 | const user = await UserFactory.create(); | ||
18 | const response = await client.get('/user/transfer').loginAs(user); | ||
19 | 15 | ||
20 | response.assertStatus(200); | 16 | response.assertStatus(200) |
21 | }); | 17 | }) |
22 | 18 | ||
23 | test('returns a validation error when not uploading a file', async ({ | 19 | test('returns a validation error when not uploading a file', async ({ client }) => { |
24 | client, | 20 | const user = await UserFactory.create() |
25 | }) => { | 21 | const response = await client.post('/user/transfer').loginAs(user) |
26 | const user = await UserFactory.create(); | ||
27 | const response = await client.post('/user/transfer').loginAs(user); | ||
28 | 22 | ||
29 | response.assertTextIncludes('Invalid Ferdium account file'); | 23 | response.assertTextIncludes('Invalid Ferdium account file') |
30 | }); | 24 | }) |
31 | 25 | ||
32 | test('returns a validation error when trying to use anything but json', async ({ | 26 | test('returns a validation error when trying to use anything but json', async ({ client }) => { |
33 | client, | 27 | const user = await UserFactory.create() |
34 | }) => { | ||
35 | const user = await UserFactory.create(); | ||
36 | const response = await client | 28 | const response = await client |
37 | .post('/user/transfer') | 29 | .post('/user/transfer') |
38 | .loginAs(user) | 30 | .loginAs(user) |
39 | .field( | 31 | .field('file', readFileSync('tests/functional/dashboard/import-stubs/random-file.txt')) |
40 | 'file', | ||
41 | readFileSync('tests/functional/dashboard/import-stubs/random-file.txt'), | ||
42 | ); | ||
43 | 32 | ||
44 | response.assertTextIncludes('Invalid Ferdium account file'); | 33 | response.assertTextIncludes('Invalid Ferdium account file') |
45 | }); | 34 | }) |
46 | 35 | ||
47 | test('returns a validation error when trying to use anything valid json', async ({ | 36 | test('returns a validation error when trying to use anything valid json', async ({ client }) => { |
48 | client, | 37 | const user = await UserFactory.create() |
49 | }) => { | ||
50 | const user = await UserFactory.create(); | ||
51 | const response = await client | 38 | const response = await client |
52 | .post('/user/transfer') | 39 | .post('/user/transfer') |
53 | .loginAs(user) | 40 | .loginAs(user) |
54 | .field( | 41 | .field('file', readFileSync('tests/functional/dashboard/import-stubs/invalid.json')) |
55 | 'file', | ||
56 | readFileSync('tests/functional/dashboard/import-stubs/invalid.json'), | ||
57 | ); | ||
58 | 42 | ||
59 | response.assertTextIncludes('Invalid Ferdium account file'); | 43 | response.assertTextIncludes('Invalid Ferdium account file') |
60 | }); | 44 | }) |
61 | 45 | ||
62 | test('returns a validation error when trying to use a json file with no ferdium structure', async ({ | 46 | test('returns a validation error when trying to use a json file with no ferdium structure', async ({ |
63 | client, | 47 | client, |
64 | }) => { | 48 | }) => { |
65 | const user = await UserFactory.create(); | 49 | const user = await UserFactory.create() |
66 | const response = await client | 50 | const response = await client |
67 | .post('/user/transfer') | 51 | .post('/user/transfer') |
68 | .loginAs(user) | 52 | .loginAs(user) |
69 | .field( | 53 | .field('file', readFileSync('tests/functional/dashboard/import-stubs/valid-no-data.json')) |
70 | 'file', | ||
71 | readFileSync( | ||
72 | 'tests/functional/dashboard/import-stubs/valid-no-data.json', | ||
73 | ), | ||
74 | ); | ||
75 | 54 | ||
76 | response.assertTextIncludes('Invalid Ferdium account file'); | 55 | response.assertTextIncludes('Invalid Ferdium account file') |
77 | }); | 56 | }) |
78 | 57 | ||
79 | test('correctly transfers data from json/ferdi-data and ferdium-data file with only workspaces', async ({ | 58 | test('correctly transfers data from json/ferdi-data and ferdium-data file with only workspaces', async ({ |
80 | client, | 59 | client, |
@@ -85,76 +64,62 @@ test.group('Dashboard / Transfer page', () => { | |||
85 | 'workspaces-only.json', | 64 | 'workspaces-only.json', |
86 | 'workspaces-only.ferdi-data', | 65 | 'workspaces-only.ferdi-data', |
87 | 'workspaces-only.ferdium-data', | 66 | 'workspaces-only.ferdium-data', |
88 | ]; | 67 | ] |
89 | 68 | ||
90 | for (const file of files) { | 69 | for (const file of files) { |
91 | // eslint-disable-next-line no-await-in-loop | 70 | // eslint-disable-next-line no-await-in-loop |
92 | const user = await UserFactory.create(); | 71 | const user = await UserFactory.create() |
93 | // eslint-disable-next-line no-await-in-loop | 72 | // eslint-disable-next-line no-await-in-loop |
94 | const response = await client | 73 | const response = await client |
95 | .post('/user/transfer') | 74 | .post('/user/transfer') |
96 | .loginAs(user) | 75 | .loginAs(user) |
97 | .field( | 76 | .field('file', readFileSync(`tests/functional/dashboard/import-stubs/${file}`)) |
98 | 'file', | 77 | |
99 | readFileSync(`tests/functional/dashboard/import-stubs/${file}`), | 78 | response.assertTextIncludes('Your account has been imported, you can now login as usual!') |
100 | ); | ||
101 | |||
102 | response.assertTextIncludes( | ||
103 | 'Your account has been imported, you can now login as usual!', | ||
104 | ); | ||
105 | // eslint-disable-next-line no-await-in-loop | 79 | // eslint-disable-next-line no-await-in-loop |
106 | await user.refresh(); | 80 | await user.refresh() |
107 | 81 | ||
108 | // eslint-disable-next-line no-await-in-loop | 82 | // eslint-disable-next-line no-await-in-loop |
109 | const workspacesForUser = await user.related('workspaces').query(); | 83 | const workspacesForUser = await user.related('workspaces').query() |
110 | assert.equal(workspacesForUser.length, 3, file); | 84 | assert.equal(workspacesForUser.length, 3, file) |
111 | 85 | ||
112 | // ensure not JSON encoded twice | 86 | // ensure not JSON encoded twice |
113 | for (const workspace of workspacesForUser) { | 87 | for (const workspace of workspacesForUser) { |
114 | assert.isNull(workspace.data.match(/\\"/), file); | 88 | assert.isNull(workspace.data.match(/\\"/), file) |
115 | } | 89 | } |
116 | } | 90 | } |
117 | }); | 91 | }) |
118 | 92 | ||
119 | test('correctly transfers data from json/ferdi-data and ferdium-data file with only services', async ({ | 93 | test('correctly transfers data from json/ferdi-data and ferdium-data file with only services', async ({ |
120 | client, | 94 | client, |
121 | assert, | 95 | assert, |
122 | }) => { | 96 | }) => { |
123 | // Repeat for all 3 file extension | 97 | // Repeat for all 3 file extension |
124 | const files = [ | 98 | const files = ['services-only.json', 'services-only.ferdi-data', 'services-only.ferdium-data'] |
125 | 'services-only.json', | ||
126 | 'services-only.ferdi-data', | ||
127 | 'services-only.ferdium-data', | ||
128 | ]; | ||
129 | 99 | ||
130 | for (const file of files) { | 100 | for (const file of files) { |
131 | // eslint-disable-next-line no-await-in-loop | 101 | // eslint-disable-next-line no-await-in-loop |
132 | const user = await UserFactory.create(); | 102 | const user = await UserFactory.create() |
133 | // eslint-disable-next-line no-await-in-loop | 103 | // eslint-disable-next-line no-await-in-loop |
134 | const response = await client | 104 | const response = await client |
135 | .post('/user/transfer') | 105 | .post('/user/transfer') |
136 | .loginAs(user) | 106 | .loginAs(user) |
137 | .field( | 107 | .field('file', readFileSync(`tests/functional/dashboard/import-stubs/${file}`)) |
138 | 'file', | 108 | |
139 | readFileSync(`tests/functional/dashboard/import-stubs/${file}`), | 109 | response.assertTextIncludes('Your account has been imported, you can now login as usual!') |
140 | ); | ||
141 | |||
142 | response.assertTextIncludes( | ||
143 | 'Your account has been imported, you can now login as usual!', | ||
144 | ); | ||
145 | // eslint-disable-next-line no-await-in-loop | 110 | // eslint-disable-next-line no-await-in-loop |
146 | await user.refresh(); | 111 | await user.refresh() |
147 | 112 | ||
148 | // eslint-disable-next-line no-await-in-loop | 113 | // eslint-disable-next-line no-await-in-loop |
149 | const servicesForUser = await user.related('services').query(); | 114 | const servicesForUser = await user.related('services').query() |
150 | assert.equal(servicesForUser.length, 3, file); | 115 | assert.equal(servicesForUser.length, 3, file) |
151 | 116 | ||
152 | // ensure not JSON encoded twice | 117 | // ensure not JSON encoded twice |
153 | for (const service of servicesForUser) { | 118 | for (const service of servicesForUser) { |
154 | assert.isNull(service.settings.match(/\\"/), file); | 119 | assert.isNull(service.settings.match(/\\"/), file) |
155 | } | 120 | } |
156 | } | 121 | } |
157 | }); | 122 | }) |
158 | 123 | ||
159 | test('correctly transfers data from json/ferdi-data and ferdium-data file with workspaces and services', async ({ | 124 | test('correctly transfers data from json/ferdi-data and ferdium-data file with workspaces and services', async ({ |
160 | client, | 125 | client, |
@@ -165,72 +130,57 @@ test.group('Dashboard / Transfer page', () => { | |||
165 | 'services-workspaces.json', | 130 | 'services-workspaces.json', |
166 | 'services-workspaces.ferdi-data', | 131 | 'services-workspaces.ferdi-data', |
167 | 'services-workspaces.ferdium-data', | 132 | 'services-workspaces.ferdium-data', |
168 | ]; | 133 | ] |
169 | 134 | ||
170 | for (const file of files) { | 135 | for (const file of files) { |
171 | // eslint-disable-next-line no-await-in-loop | 136 | // eslint-disable-next-line no-await-in-loop |
172 | const user = await UserFactory.create(); | 137 | const user = await UserFactory.create() |
173 | // eslint-disable-next-line no-await-in-loop | 138 | // eslint-disable-next-line no-await-in-loop |
174 | const response = await client | 139 | const response = await client |
175 | .post('/user/transfer') | 140 | .post('/user/transfer') |
176 | .loginAs(user) | 141 | .loginAs(user) |
177 | .field( | 142 | .field('file', readFileSync(`tests/functional/dashboard/import-stubs/${file}`)) |
178 | 'file', | 143 | |
179 | readFileSync(`tests/functional/dashboard/import-stubs/${file}`), | 144 | response.assertTextIncludes('Your account has been imported, you can now login as usual!') |
180 | ); | ||
181 | |||
182 | response.assertTextIncludes( | ||
183 | 'Your account has been imported, you can now login as usual!', | ||
184 | ); | ||
185 | // eslint-disable-next-line no-await-in-loop | 145 | // eslint-disable-next-line no-await-in-loop |
186 | await user.refresh(); | 146 | await user.refresh() |
187 | 147 | ||
188 | // eslint-disable-next-line no-await-in-loop | 148 | // eslint-disable-next-line no-await-in-loop |
189 | const servicesForUser = await user.related('services').query(); | 149 | const servicesForUser = await user.related('services').query() |
190 | // eslint-disable-next-line no-await-in-loop | 150 | // eslint-disable-next-line no-await-in-loop |
191 | const workspacesForUser = await user.related('workspaces').query(); | 151 | const workspacesForUser = await user.related('workspaces').query() |
192 | assert.equal(servicesForUser.length, 3, file); | 152 | assert.equal(servicesForUser.length, 3, file) |
193 | assert.equal(workspacesForUser.length, 3, file); | 153 | assert.equal(workspacesForUser.length, 3, file) |
194 | 154 | ||
195 | const firstServiceUuid = servicesForUser.find( | 155 | const firstServiceUuid = servicesForUser.find((s) => s.name === 'random-service-1')?.serviceId |
196 | s => s.name === 'random-service-1', | ||
197 | )?.serviceId; | ||
198 | const secondServiceUuid = servicesForUser.find( | 156 | const secondServiceUuid = servicesForUser.find( |
199 | s => s.name === 'random-service-2', | 157 | (s) => s.name === 'random-service-2' |
200 | )?.serviceId; | 158 | )?.serviceId |
201 | const thirdServiceUUid = servicesForUser.find( | 159 | const thirdServiceUUid = servicesForUser.find((s) => s.name === 'random-service-3')?.serviceId |
202 | s => s.name === 'random-service-3', | ||
203 | )?.serviceId; | ||
204 | 160 | ||
205 | // Assert the first workspace to not have any services | 161 | // Assert the first workspace to not have any services |
206 | const firstWorkspace = workspacesForUser.find( | 162 | const firstWorkspace = workspacesForUser.find((w) => w.name === 'workspace1') |
207 | w => w.name === 'workspace1', | ||
208 | ); | ||
209 | if (firstWorkspace?.services) { | 163 | if (firstWorkspace?.services) { |
210 | assert.empty(JSON.parse(firstWorkspace.services), file); | 164 | assert.empty(JSON.parse(firstWorkspace.services), file) |
211 | } | 165 | } |
212 | 166 | ||
213 | const secondWorkspace = workspacesForUser.find( | 167 | const secondWorkspace = workspacesForUser.find((w) => w.name === 'workspace2') |
214 | w => w.name === 'workspace2', | ||
215 | ); | ||
216 | if (secondWorkspace?.services) { | 168 | if (secondWorkspace?.services) { |
217 | assert.deepEqual( | 169 | assert.deepEqual( |
218 | JSON.parse(secondWorkspace.services), | 170 | JSON.parse(secondWorkspace.services), |
219 | [firstServiceUuid, secondServiceUuid], | 171 | [firstServiceUuid, secondServiceUuid], |
220 | file, | 172 | file |
221 | ); | 173 | ) |
222 | } | 174 | } |
223 | 175 | ||
224 | const thirdWorkspace = workspacesForUser.find( | 176 | const thirdWorkspace = workspacesForUser.find((w) => w.name === 'workspace3') |
225 | w => w.name === 'workspace3', | ||
226 | ); | ||
227 | if (thirdWorkspace?.services) { | 177 | if (thirdWorkspace?.services) { |
228 | assert.deepEqual( | 178 | assert.deepEqual( |
229 | JSON.parse(thirdWorkspace.services), | 179 | JSON.parse(thirdWorkspace.services), |
230 | [firstServiceUuid, secondServiceUuid, thirdServiceUUid], | 180 | [firstServiceUuid, secondServiceUuid, thirdServiceUUid], |
231 | file, | 181 | file |
232 | ); | 182 | ) |
233 | } | 183 | } |
234 | } | 184 | } |
235 | }); | 185 | }) |
236 | }); | 186 | }) |