1
1
import { Test , TestingModule } from '@nestjs/testing' ;
2
2
import {
3
- mockAppSettings , mockAppSettingsInitial , mockAppSettingsWithoutPermissions ,
3
+ mockAppSettings ,
4
+ mockAppSettingsInitial ,
5
+ mockAppSettingsWithoutPermissions ,
4
6
mockEncryptionStrategyInstance ,
5
7
mockEncryptResult ,
8
+ mockKeyEncryptionStrategyInstance ,
9
+ mockKeyEncryptResult ,
6
10
mockSettingsService ,
7
11
MockType ,
8
12
} from 'src/__mocks__' ;
@@ -12,11 +16,13 @@ import { KeytarEncryptionStrategy } from 'src/modules/encryption/strategies/keyt
12
16
import { EncryptionStrategy } from 'src/modules/encryption/models' ;
13
17
import { UnsupportedEncryptionStrategyException } from 'src/modules/encryption/exceptions' ;
14
18
import { SettingsService } from 'src/modules/settings/settings.service' ;
19
+ import { KeyEncryptionStrategy } from 'src/modules/encryption/strategies/key-encryption.strategy' ;
15
20
16
21
describe ( 'EncryptionService' , ( ) => {
17
22
let service : EncryptionService ;
18
23
let plainEncryptionStrategy : MockType < PlainEncryptionStrategy > ;
19
24
let keytarEncryptionStrategy : MockType < KeytarEncryptionStrategy > ;
25
+ let keyEncryptionStrategy : MockType < KeyEncryptionStrategy > ;
20
26
let settingsService : MockType < SettingsService > ;
21
27
22
28
beforeEach ( async ( ) => {
@@ -33,6 +39,10 @@ describe('EncryptionService', () => {
33
39
provide : KeytarEncryptionStrategy ,
34
40
useFactory : mockEncryptionStrategyInstance ,
35
41
} ,
42
+ {
43
+ provide : KeyEncryptionStrategy ,
44
+ useFactory : mockKeyEncryptionStrategyInstance ,
45
+ } ,
36
46
{
37
47
provide : SettingsService ,
38
48
useFactory : mockSettingsService ,
@@ -43,22 +53,43 @@ describe('EncryptionService', () => {
43
53
service = module . get ( EncryptionService ) ;
44
54
plainEncryptionStrategy = module . get ( PlainEncryptionStrategy ) ;
45
55
keytarEncryptionStrategy = module . get ( KeytarEncryptionStrategy ) ;
56
+ keyEncryptionStrategy = module . get ( KeyEncryptionStrategy ) ;
46
57
settingsService = module . get ( SettingsService ) ;
47
58
48
59
settingsService . getAppSettings . mockResolvedValue ( mockAppSettings ) ;
49
60
} ) ;
50
61
51
62
describe ( 'getAvailableEncryptionStrategies' , ( ) => {
52
- it ( 'Should return list 2 strategies available' , async ( ) => {
63
+ it ( 'Should return list 2 strategies available (KEYTAR and PLAIN) ' , async ( ) => {
53
64
keytarEncryptionStrategy . isAvailable . mockResolvedValueOnce ( true ) ;
65
+ keyEncryptionStrategy . isAvailable . mockResolvedValueOnce ( false ) ;
54
66
55
67
expect ( await service . getAvailableEncryptionStrategies ( ) ) . toEqual ( [
56
68
EncryptionStrategy . PLAIN ,
57
69
EncryptionStrategy . KEYTAR ,
58
70
] ) ;
59
71
} ) ;
72
+ it ( 'Should return list 2 strategies available (KEY and PLAIN)' , async ( ) => {
73
+ keytarEncryptionStrategy . isAvailable . mockResolvedValueOnce ( false ) ;
74
+ keyEncryptionStrategy . isAvailable . mockResolvedValueOnce ( true ) ;
75
+
76
+ expect ( await service . getAvailableEncryptionStrategies ( ) ) . toEqual ( [
77
+ EncryptionStrategy . PLAIN ,
78
+ EncryptionStrategy . KEY ,
79
+ ] ) ;
80
+ } ) ;
81
+ it ( 'Should return list 2 strategies available (KEY and PLAIN) even when KEYTAR available' , async ( ) => {
82
+ keytarEncryptionStrategy . isAvailable . mockResolvedValueOnce ( true ) ;
83
+ keyEncryptionStrategy . isAvailable . mockResolvedValueOnce ( true ) ;
84
+
85
+ expect ( await service . getAvailableEncryptionStrategies ( ) ) . toEqual ( [
86
+ EncryptionStrategy . PLAIN ,
87
+ EncryptionStrategy . KEY ,
88
+ ] ) ;
89
+ } ) ;
60
90
it ( 'Should return list with one strategy available' , async ( ) => {
61
91
keytarEncryptionStrategy . isAvailable . mockResolvedValueOnce ( false ) ;
92
+ keyEncryptionStrategy . isAvailable . mockResolvedValueOnce ( false ) ;
62
93
63
94
expect ( await service . getAvailableEncryptionStrategies ( ) ) . toEqual ( [
64
95
EncryptionStrategy . PLAIN ,
@@ -70,7 +101,15 @@ describe('EncryptionService', () => {
70
101
it ( 'Should return KEYTAR strategy based on app agreements' , async ( ) => {
71
102
expect ( await service . getEncryptionStrategy ( ) ) . toEqual ( keytarEncryptionStrategy ) ;
72
103
} ) ;
73
- it ( 'Should return PLAIN strategy based on app agreements' , async ( ) => {
104
+ it ( 'Should return KEY strategy based on app agreements even when KEYTAR available' , async ( ) => {
105
+ keytarEncryptionStrategy . isAvailable . mockResolvedValueOnce ( true ) ;
106
+ keyEncryptionStrategy . isAvailable . mockResolvedValueOnce ( true ) ;
107
+
108
+ expect ( await service . getEncryptionStrategy ( ) ) . toEqual ( keyEncryptionStrategy ) ;
109
+ } ) ;
110
+ it ( 'Should return PLAIN strategy based on app agreements even when KEY available' , async ( ) => {
111
+ keyEncryptionStrategy . isAvailable . mockResolvedValueOnce ( true ) ;
112
+
74
113
settingsService . getAppSettings . mockResolvedValueOnce ( mockAppSettingsWithoutPermissions ) ;
75
114
76
115
expect ( await service . getEncryptionStrategy ( ) ) . toEqual ( plainEncryptionStrategy ) ;
@@ -83,19 +122,37 @@ describe('EncryptionService', () => {
83
122
} ) ;
84
123
85
124
describe ( 'encrypt' , ( ) => {
86
- it ( 'Should encrypt data and return proper response' , async ( ) => {
125
+ it ( 'Should encrypt data and return proper response (KEYTAR)' , async ( ) => {
126
+ keyEncryptionStrategy . isAvailable . mockResolvedValueOnce ( false ) ;
127
+
87
128
keytarEncryptionStrategy . encrypt . mockResolvedValueOnce ( mockEncryptResult ) ;
88
129
89
130
expect ( await service . encrypt ( 'string' ) ) . toEqual ( mockEncryptResult ) ;
90
131
} ) ;
132
+ it ( 'Should encrypt data and return proper response (KEY)' , async ( ) => {
133
+ keyEncryptionStrategy . isAvailable . mockResolvedValueOnce ( true ) ;
134
+
135
+ keyEncryptionStrategy . encrypt . mockResolvedValueOnce ( mockKeyEncryptResult ) ;
136
+
137
+ expect ( await service . encrypt ( 'string' ) ) . toEqual ( mockKeyEncryptResult ) ;
138
+ } ) ;
91
139
} ) ;
92
140
93
141
describe ( 'decrypt' , ( ) => {
94
- it ( 'Should return decrypted string' , async ( ) => {
142
+ it ( 'Should return decrypted string (KEYTAR)' , async ( ) => {
143
+ keyEncryptionStrategy . isAvailable . mockResolvedValueOnce ( false ) ;
144
+
95
145
keytarEncryptionStrategy . decrypt . mockResolvedValueOnce ( mockEncryptResult . data ) ;
96
146
97
147
expect ( await service . decrypt ( 'string' , EncryptionStrategy . KEYTAR ) ) . toEqual ( mockEncryptResult . data ) ;
98
148
} ) ;
149
+ it ( 'Should return decrypted string (KEY)' , async ( ) => {
150
+ keyEncryptionStrategy . isAvailable . mockResolvedValueOnce ( true ) ;
151
+
152
+ keyEncryptionStrategy . decrypt . mockResolvedValueOnce ( mockKeyEncryptResult . data ) ;
153
+
154
+ expect ( await service . decrypt ( 'string' , EncryptionStrategy . KEY ) ) . toEqual ( mockKeyEncryptResult . data ) ;
155
+ } ) ;
99
156
it ( 'Should return null when no data passed' , async ( ) => {
100
157
expect ( await service . decrypt ( null , EncryptionStrategy . KEYTAR ) ) . toEqual ( null ) ;
101
158
} ) ;
0 commit comments