33using Bit . Core . Exceptions ;
44using Bit . Core . KeyManagement . Commands ;
55using Bit . Core . KeyManagement . Models . Api . Request ;
6+ using Bit . Core . KeyManagement . Models . Data ;
67using Bit . Core . KeyManagement . Queries . Interfaces ;
78using Bit . Core . OrganizationFeatures . OrganizationUsers . Interfaces ;
9+ using Bit . Core . Repositories ;
810using Bit . Core . Services ;
911using Bit . Test . Common . AutoFixture ;
1012using Bit . Test . Common . AutoFixture . Attributes ;
@@ -23,33 +25,47 @@ public async Task SetKeyConnectorKeyForUserAsync_Success_SetsAccountKeys(
2325 SetKeyConnectorKeyRequestModel requestModel ,
2426 SutProvider < SetKeyConnectorKeyCommand > sutProvider )
2527 {
28+ // Set up valid V2 encryption data
29+ if ( requestModel . AccountKeys ! . SignatureKeyPair != null )
30+ {
31+ requestModel . AccountKeys . SignatureKeyPair . SignatureAlgorithm = "ed25519" ;
32+ }
33+
34+ var expectedAccountKeysData = requestModel . AccountKeys . ToAccountKeysData ( ) ;
35+
2636 // Arrange
27- var originalRevisionDate = user . RevisionDate ;
28- var originalAccountRevisionDate = user . AccountRevisionDate ;
29- var expectedTime = DateTime . UtcNow ;
37+ var userRepository = sutProvider . GetDependency < IUserRepository > ( ) ;
38+ var mockUpdateUserData = Substitute . For < UpdateUserData > ( ) ;
39+ userRepository . SetKeyConnectorUserKey ( user . Id , requestModel . KeyConnectorKeyWrappedUserKey ! )
40+ . Returns ( mockUpdateUserData ) ;
3041
3142 // Act
3243 await sutProvider . Sut . SetKeyConnectorKeyForUserAsync ( user , requestModel ) ;
3344
3445 // Assert
35- Assert . Equal ( requestModel . KeyConnectorKeyWrappedUserKey , user . Key ) ;
36- Assert . True ( user . UsesKeyConnector ) ;
37- Assert . Equal ( KdfType . Argon2id , user . Kdf ) ;
38- Assert . Equal ( 3 , user . KdfIterations ) ;
39- Assert . Equal ( 64 , user . KdfMemory ) ;
40- Assert . Equal ( 4 , user . KdfParallelism ) ;
41- Assert . NotEqual ( originalRevisionDate , user . RevisionDate ) ;
42- Assert . NotEqual ( originalAccountRevisionDate , user . AccountRevisionDate ) ;
43- Assert . Equal ( expectedTime , user . RevisionDate , precision : TimeSpan . FromMinutes ( 1 ) ) ;
44- Assert . Equal ( expectedTime , user . AccountRevisionDate , precision : TimeSpan . FromMinutes ( 1 ) ) ;
45-
4646 sutProvider . GetDependency < ICanUseKeyConnectorQuery > ( )
4747 . Received ( 1 )
4848 . VerifyCanUseKeyConnector ( user ) ;
4949
50- await sutProvider . GetDependency < ISetAccountKeysForUserCommand > ( )
50+ userRepository
5151 . Received ( 1 )
52- . SetAccountKeysForUserAsync ( user , requestModel . AccountKeys ) ;
52+ . SetKeyConnectorUserKey ( user . Id , requestModel . KeyConnectorKeyWrappedUserKey ) ;
53+
54+ await userRepository
55+ . Received ( 1 )
56+ . SetV2AccountCryptographicStateAsync (
57+ user . Id ,
58+ Arg . Is < UserAccountKeysData > ( data =>
59+ data . PublicKeyEncryptionKeyPairData . PublicKey == expectedAccountKeysData . PublicKeyEncryptionKeyPairData . PublicKey &&
60+ data . PublicKeyEncryptionKeyPairData . WrappedPrivateKey == expectedAccountKeysData . PublicKeyEncryptionKeyPairData . WrappedPrivateKey &&
61+ data . PublicKeyEncryptionKeyPairData . SignedPublicKey == expectedAccountKeysData . PublicKeyEncryptionKeyPairData . SignedPublicKey &&
62+ data . SignatureKeyPairData ! . SignatureAlgorithm == expectedAccountKeysData . SignatureKeyPairData ! . SignatureAlgorithm &&
63+ data . SignatureKeyPairData . WrappedSigningKey == expectedAccountKeysData . SignatureKeyPairData . WrappedSigningKey &&
64+ data . SignatureKeyPairData . VerifyingKey == expectedAccountKeysData . SignatureKeyPairData . VerifyingKey &&
65+ data . SecurityStateData ! . SecurityState == expectedAccountKeysData . SecurityStateData ! . SecurityState &&
66+ data . SecurityStateData . SecurityVersion == expectedAccountKeysData . SecurityStateData . SecurityVersion ) ,
67+ Arg . Is < IEnumerable < UpdateUserData > > ( actions =>
68+ actions . Count ( ) == 1 && actions . First ( ) == mockUpdateUserData ) ) ;
5369
5470 await sutProvider . GetDependency < IEventService > ( )
5571 . Received ( 1 )
@@ -75,9 +91,13 @@ public async Task SetKeyConnectorKeyForUserAsync_NullKeyConnectorKeyWrappedUserK
7591
7692 Assert . Equal ( "KeyConnectorKeyWrappedUserKey and AccountKeys must be provided" , exception . Message ) ;
7793
78- await sutProvider . GetDependency < ISetAccountKeysForUserCommand > ( )
94+ sutProvider . GetDependency < IUserRepository > ( )
95+ . DidNotReceiveWithAnyArgs ( )
96+ . SetKeyConnectorUserKey ( Arg . Any < Guid > ( ) , Arg . Any < string > ( ) ) ;
97+
98+ await sutProvider . GetDependency < IUserRepository > ( )
7999 . DidNotReceiveWithAnyArgs ( )
80- . SetAccountKeysForUserAsync ( Arg . Any < User > ( ) , Arg . Any < AccountKeysRequestModel > ( ) ) ;
100+ . SetV2AccountCryptographicStateAsync ( Arg . Any < Guid > ( ) , Arg . Any < UserAccountKeysData > ( ) , Arg . Any < IEnumerable < UpdateUserData > > ( ) ) ;
81101
82102 await sutProvider . GetDependency < IEventService > ( )
83103 . DidNotReceiveWithAnyArgs ( )
@@ -103,9 +123,13 @@ public async Task SetKeyConnectorKeyForUserAsync_EmptyKeyConnectorKeyWrappedUser
103123
104124 Assert . Equal ( "KeyConnectorKeyWrappedUserKey and AccountKeys must be provided" , exception . Message ) ;
105125
106- await sutProvider . GetDependency < ISetAccountKeysForUserCommand > ( )
126+ sutProvider . GetDependency < IUserRepository > ( )
107127 . DidNotReceiveWithAnyArgs ( )
108- . SetAccountKeysForUserAsync ( Arg . Any < User > ( ) , Arg . Any < AccountKeysRequestModel > ( ) ) ;
128+ . SetKeyConnectorUserKey ( Arg . Any < Guid > ( ) , Arg . Any < string > ( ) ) ;
129+
130+ await sutProvider . GetDependency < IUserRepository > ( )
131+ . DidNotReceiveWithAnyArgs ( )
132+ . SetV2AccountCryptographicStateAsync ( Arg . Any < Guid > ( ) , Arg . Any < UserAccountKeysData > ( ) , Arg . Any < IEnumerable < UpdateUserData > > ( ) ) ;
109133
110134 await sutProvider . GetDependency < IEventService > ( )
111135 . DidNotReceiveWithAnyArgs ( )
@@ -131,9 +155,13 @@ public async Task SetKeyConnectorKeyForUserAsync_NullAccountKeys_ThrowsBadReques
131155
132156 Assert . Equal ( "KeyConnectorKeyWrappedUserKey and AccountKeys must be provided" , exception . Message ) ;
133157
134- await sutProvider . GetDependency < ISetAccountKeysForUserCommand > ( )
158+ sutProvider . GetDependency < IUserRepository > ( )
159+ . DidNotReceiveWithAnyArgs ( )
160+ . SetKeyConnectorUserKey ( Arg . Any < Guid > ( ) , Arg . Any < string > ( ) ) ;
161+
162+ await sutProvider . GetDependency < IUserRepository > ( )
135163 . DidNotReceiveWithAnyArgs ( )
136- . SetAccountKeysForUserAsync ( Arg . Any < User > ( ) , Arg . Any < AccountKeysRequestModel > ( ) ) ;
164+ . SetV2AccountCryptographicStateAsync ( Arg . Any < Guid > ( ) , Arg . Any < UserAccountKeysData > ( ) , Arg . Any < IEnumerable < UpdateUserData > > ( ) ) ;
137165
138166 await sutProvider . GetDependency < IEventService > ( )
139167 . DidNotReceiveWithAnyArgs ( )
@@ -162,9 +190,13 @@ public async Task SetKeyConnectorKeyForUserAsync_UserCannotUseKeyConnector_Throw
162190
163191 Assert . Equal ( expectedException . Message , exception . Message ) ;
164192
165- await sutProvider . GetDependency < ISetAccountKeysForUserCommand > ( )
193+ sutProvider . GetDependency < IUserRepository > ( )
194+ . DidNotReceiveWithAnyArgs ( )
195+ . SetKeyConnectorUserKey ( Arg . Any < Guid > ( ) , Arg . Any < string > ( ) ) ;
196+
197+ await sutProvider . GetDependency < IUserRepository > ( )
166198 . DidNotReceiveWithAnyArgs ( )
167- . SetAccountKeysForUserAsync ( Arg . Any < User > ( ) , Arg . Any < AccountKeysRequestModel > ( ) ) ;
199+ . SetV2AccountCryptographicStateAsync ( Arg . Any < Guid > ( ) , Arg . Any < UserAccountKeysData > ( ) , Arg . Any < IEnumerable < UpdateUserData > > ( ) ) ;
168200
169201 await sutProvider . GetDependency < IEventService > ( )
170202 . DidNotReceiveWithAnyArgs ( )
0 commit comments