diff options
Diffstat (limited to 'packages/shared/src/stores/__tests__/ServiceBase.test.ts')
-rw-r--r-- | packages/shared/src/stores/__tests__/ServiceBase.test.ts | 153 |
1 files changed, 153 insertions, 0 deletions
diff --git a/packages/shared/src/stores/__tests__/ServiceBase.test.ts b/packages/shared/src/stores/__tests__/ServiceBase.test.ts new file mode 100644 index 0000000..2fc9cbc --- /dev/null +++ b/packages/shared/src/stores/__tests__/ServiceBase.test.ts | |||
@@ -0,0 +1,153 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2022 Kristóf Marussy <kristof@marussy.com> | ||
3 | * | ||
4 | * This file is part of Sophie. | ||
5 | * | ||
6 | * Sophie is free software: you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU Affero General Public License as | ||
8 | * published by the Free Software Foundation, version 3. | ||
9 | * | ||
10 | * This program is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | * GNU Affero General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU Affero General Public License | ||
16 | * along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
17 | * | ||
18 | * SPDX-License-Identifier: AGPL-3.0-only | ||
19 | */ | ||
20 | |||
21 | import type { SnapshotIn } from 'mobx-state-tree'; | ||
22 | |||
23 | import ServiceBase, { SecurityLabelKind } from '../ServiceBase.js'; | ||
24 | import type { ServiceStateSnapshotIn } from '../ServiceState.js'; | ||
25 | import SharedStoreBase from '../SharedStoreBase.js'; | ||
26 | |||
27 | import { | ||
28 | testCertificate, | ||
29 | testProfile, | ||
30 | testService, | ||
31 | } from './__fixtures__/serviceFixtures.js'; | ||
32 | |||
33 | function createTestService( | ||
34 | snapshot: Partial<SnapshotIn<typeof ServiceBase>>, | ||
35 | ): ServiceBase { | ||
36 | const sharedStore = SharedStoreBase.create({ | ||
37 | profilesById: { | ||
38 | testProfile, | ||
39 | }, | ||
40 | servicesById: { | ||
41 | testService: { | ||
42 | ...testService, | ||
43 | ...snapshot, | ||
44 | }, | ||
45 | }, | ||
46 | }); | ||
47 | return sharedStore.servicesById.get(testService.id)!; | ||
48 | } | ||
49 | |||
50 | test.each<{ | ||
51 | state: ServiceStateSnapshotIn; | ||
52 | loading: boolean; | ||
53 | crashed: boolean; | ||
54 | hasError: boolean; | ||
55 | }>([ | ||
56 | { | ||
57 | state: { type: 'initializing' }, | ||
58 | loading: true, | ||
59 | crashed: false, | ||
60 | hasError: false, | ||
61 | }, | ||
62 | { | ||
63 | state: { type: 'loading' }, | ||
64 | loading: true, | ||
65 | crashed: false, | ||
66 | hasError: false, | ||
67 | }, | ||
68 | { | ||
69 | state: { type: 'loaded' }, | ||
70 | loading: false, | ||
71 | crashed: false, | ||
72 | hasError: false, | ||
73 | }, | ||
74 | { | ||
75 | state: { | ||
76 | type: 'failed', | ||
77 | errorCode: 999, | ||
78 | errorDesc: 'Test error', | ||
79 | }, | ||
80 | loading: false, | ||
81 | crashed: false, | ||
82 | hasError: true, | ||
83 | }, | ||
84 | { | ||
85 | state: { | ||
86 | type: 'certificateError', | ||
87 | errorCode: 'Test certificate error', | ||
88 | certificate: testCertificate, | ||
89 | trust: 'pending', | ||
90 | }, | ||
91 | loading: false, | ||
92 | crashed: false, | ||
93 | hasError: true, | ||
94 | }, | ||
95 | { | ||
96 | state: { | ||
97 | type: 'crashed', | ||
98 | reason: 'Test', | ||
99 | exitCode: 255, | ||
100 | }, | ||
101 | loading: false, | ||
102 | crashed: true, | ||
103 | hasError: true, | ||
104 | }, | ||
105 | ])( | ||
106 | 'sets the loaded and error flags when the status is $state', | ||
107 | ({ state, loading, crashed, hasError }) => { | ||
108 | const service = createTestService({ state }); | ||
109 | |||
110 | expect(service.loading).toBe(loading); | ||
111 | expect(service.crashed).toBe(crashed); | ||
112 | expect(service.hasError).toBe(hasError); | ||
113 | expect(service.alwaysShowLocationBar).toBe(hasError); | ||
114 | }, | ||
115 | ); | ||
116 | |||
117 | test.each([ | ||
118 | [undefined, SecurityLabelKind.Empty, false], | ||
119 | ['', SecurityLabelKind.Empty, false], | ||
120 | ['https://example.org', SecurityLabelKind.SecureConnection, false], | ||
121 | ['http://example.org', SecurityLabelKind.NotSecureConnection, true], | ||
122 | ['https:', SecurityLabelKind.InvalidURL, true], | ||
123 | ['file:///etc/shadow', SecurityLabelKind.InvalidURL, true], | ||
124 | ['asdfasdfasdf', SecurityLabelKind.InvalidURL, true], | ||
125 | ])( | ||
126 | 'sets the security label when the url %s is loaded', | ||
127 | ( | ||
128 | url: string | undefined, | ||
129 | labelKind: SecurityLabelKind, | ||
130 | hasWarning: boolean, | ||
131 | ) => { | ||
132 | const service = createTestService({ currentUrl: url }); | ||
133 | |||
134 | expect(service.securityLabel).toBe(labelKind); | ||
135 | expect(service.hasSecurityLabelWarning).toBe(hasWarning); | ||
136 | expect(service.alwaysShowLocationBar).toBe(hasWarning); | ||
137 | }, | ||
138 | ); | ||
139 | |||
140 | test('shows a certificate error warning if there is a certificate error', () => { | ||
141 | const service = createTestService({ | ||
142 | state: { | ||
143 | type: 'certificateError', | ||
144 | errorCode: 'Test certificate error', | ||
145 | certificate: testCertificate, | ||
146 | trust: 'pending', | ||
147 | }, | ||
148 | }); | ||
149 | |||
150 | expect(service.securityLabel).toBe(SecurityLabelKind.CertificateError); | ||
151 | expect(service.hasSecurityLabelWarning).toBe(true); | ||
152 | expect(service.alwaysShowLocationBar).toBe(true); | ||
153 | }); | ||