@@ -63,16 +63,24 @@ describe('AccountService', () => {
6363 } ) ;
6464
6565 describe ( 'deleteAccount' , ( ) => {
66- it ( 'when account has a principal name, then deletes from provider and DB' , async ( ) => {
67- const attrs = newMailAccountAttributes ( ) ;
68- const account = MailAccount . build ( attrs ) ;
66+ it ( 'when account has addresses, then deletes all principals and account' , async ( ) => {
67+ const addr1 = newMailAddressAttributes ( { isDefault : true } ) ;
68+ const addr2 = newMailAddressAttributes ( { isDefault : false } ) ;
69+ const account = MailAccount . build (
70+ newMailAccountAttributes ( { addresses : [ addr1 , addr2 ] } ) ,
71+ ) ;
6972 accounts . findByUserId . mockResolvedValue ( account ) ;
7073
71- await service . deleteAccount ( attrs . userId ) ;
74+ await service . deleteAccount ( account . userId ) ;
7275
7376 expect ( provider . deleteAccount ) . toHaveBeenCalledWith (
74- account . providerAccountId ,
77+ addr1 . providerExternalId ,
78+ ) ;
79+ expect ( provider . deleteAccount ) . toHaveBeenCalledWith (
80+ addr2 . providerExternalId ,
7581 ) ;
82+ expect ( addresses . deleteProviderLink ) . toHaveBeenCalledWith ( addr1 . id ) ;
83+ expect ( addresses . deleteProviderLink ) . toHaveBeenCalledWith ( addr2 . id ) ;
7684 expect ( accounts . delete ) . toHaveBeenCalledWith ( account . id ) ;
7785 } ) ;
7886
@@ -86,7 +94,7 @@ describe('AccountService', () => {
8694 } ) ;
8795
8896 describe ( 'addAddress' , ( ) => {
89- it ( 'when all conditions met, then creates address and links provider' , async ( ) => {
97+ it ( 'when all conditions met, then creates principal and links provider' , async ( ) => {
9098 const accountAttrs = newMailAccountAttributes ( ) ;
9199 const account = MailAccount . build ( accountAttrs ) ;
92100 const domainAttrs = newMailDomainAttributes ( ) ;
@@ -103,6 +111,8 @@ describe('AccountService', () => {
103111 accountAttrs . userId ,
104112 newAddr ,
105113 domainAttrs . domain ,
114+ 'password123' ,
115+ 'Display Name' ,
106116 ) ;
107117
108118 expect ( addresses . create ) . toHaveBeenCalledWith ( {
@@ -111,14 +121,16 @@ describe('AccountService', () => {
111121 domainId : domain . id ,
112122 isDefault : false ,
113123 } ) ;
114- expect ( provider . addAddress ) . toHaveBeenCalledWith (
115- account . providerAccountId ,
116- newAddr ,
117- ) ;
124+ expect ( provider . createAccount ) . toHaveBeenCalledWith ( {
125+ accountId : newAddressId ,
126+ primaryAddress : newAddr ,
127+ displayName : 'Display Name' ,
128+ password : 'password123' ,
129+ } ) ;
118130 expect ( addresses . createProviderLink ) . toHaveBeenCalledWith ( {
119131 mailAddressId : newAddressId ,
120132 provider : 'stalwart' ,
121- externalId : account . providerAccountId ,
133+ externalId : newAddr ,
122134 } ) ;
123135 } ) ;
124136
@@ -130,7 +142,7 @@ describe('AccountService', () => {
130142 addresses . findByAddress . mockResolvedValue ( null ) ;
131143
132144 await expect (
133- service . addAddress ( 'unknown' , 'a@b.com' , 'b.com' ) ,
145+ service . addAddress ( 'unknown' , 'a@b.com' , 'b.com' , 'pass' ) ,
134146 ) . rejects . toThrow ( NotFoundException ) ;
135147 } ) ;
136148
@@ -141,7 +153,7 @@ describe('AccountService', () => {
141153 addresses . findByAddress . mockResolvedValue ( null ) ;
142154
143155 await expect (
144- service . addAddress ( account . userId , 'a@b.com' , 'unknown.com' ) ,
156+ service . addAddress ( account . userId , 'a@b.com' , 'unknown.com' , 'pass' ) ,
145157 ) . rejects . toThrow ( NotFoundException ) ;
146158 } ) ;
147159
@@ -155,7 +167,12 @@ describe('AccountService', () => {
155167 addresses . findByAddress . mockResolvedValue ( existing ) ;
156168
157169 await expect (
158- service . addAddress ( account . userId , existing . address , domain . domain ) ,
170+ service . addAddress (
171+ account . userId ,
172+ existing . address ,
173+ domain . domain ,
174+ 'pass' ,
175+ ) ,
159176 ) . rejects . toThrow ( ConflictException ) ;
160177 } ) ;
161178
@@ -168,10 +185,15 @@ describe('AccountService', () => {
168185 domains . findByDomain . mockResolvedValue ( domain ) ;
169186 addresses . findByAddress . mockResolvedValue ( null ) ;
170187 addresses . create . mockResolvedValue ( newAddressId ) ;
171- provider . addAddress . mockRejectedValue ( new Error ( 'provider down' ) ) ;
188+ provider . createAccount . mockRejectedValue ( new Error ( 'provider down' ) ) ;
172189
173190 await expect (
174- service . addAddress ( account . userId , 'new@example.com' , domain . domain ) ,
191+ service . addAddress (
192+ account . userId ,
193+ 'new@example.com' ,
194+ domain . domain ,
195+ 'pass' ,
196+ ) ,
175197 ) . rejects . toThrow ( 'provider down' ) ;
176198
177199 expect ( addresses . delete ) . toHaveBeenCalledWith ( newAddressId ) ;
@@ -180,7 +202,7 @@ describe('AccountService', () => {
180202 } ) ;
181203
182204 describe ( 'removeAddress' , ( ) => {
183- it ( 'when address exists and is not default, then removes it ' , async ( ) => {
205+ it ( 'when address exists and is not default, then deletes principal and address ' , async ( ) => {
184206 const nonDefaultAddr = newMailAddressAttributes ( { isDefault : false } ) ;
185207 const account = MailAccount . build (
186208 newMailAccountAttributes ( {
@@ -194,9 +216,8 @@ describe('AccountService', () => {
194216
195217 await service . removeAddress ( account . userId , nonDefaultAddr . address ) ;
196218
197- expect ( provider . removeAddress ) . toHaveBeenCalledWith (
198- account . providerAccountId ,
199- nonDefaultAddr . address ,
219+ expect ( provider . deleteAccount ) . toHaveBeenCalledWith (
220+ nonDefaultAddr . providerExternalId ,
200221 ) ;
201222 expect ( addresses . deleteProviderLink ) . toHaveBeenCalledWith (
202223 nonDefaultAddr . id ,
0 commit comments