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