@@ -214,4 +214,111 @@ public void Check_ReturnsNoContent_WhenAuthenticated()
214214 // Assert
215215 Assert . That ( result , Is . TypeOf < NoContentResult > ( ) ) ;
216216 }
217+
218+ [ Test ]
219+ public async Task Login_IncludesAccountIds_WhenUserIsManager ( )
220+ {
221+ // Arrange
222+ var account1 = new Account { Id = 1 , Name = "Test Account 1" } ;
223+ var account2 = new Account { Id = 2 , Name = "Test Account 2" } ;
224+ _dbContext . Accounts . AddRange ( account1 , account2 ) ;
225+
226+ var managerRole = new Role { Id = 100 , RoleId = UserRoleConstants . AccountManager , Name = "Manager" } ;
227+ _dbContext . Roles . Add ( managerRole ) ;
228+
229+ string hashedPassword = BCrypt . Net . BCrypt . HashPassword ( "password123" ) ;
230+ var managerUser = new User
231+ {
232+ Id = 2 ,
233+ Email = "manager@example.com" ,
234+ Password = hashedPassword ,
235+ FirstName = "Manager" ,
236+ LastName = "User" ,
237+ Patronymic = "" ,
238+ UserRoles =
239+ [
240+ new UserRole { UserId = 2 , RoleId = managerRole . Id , Role = managerRole }
241+ ] ,
242+ UserAccounts =
243+ [
244+ new UserAccount { UserId = 2 , AccountId = account1 . Id , Account = account1 } ,
245+ new UserAccount { UserId = 2 , AccountId = account2 . Id , Account = account2 }
246+ ]
247+ } ;
248+
249+ _dbContext . Users . Add ( managerUser ) ;
250+ await _dbContext . SaveChangesAsync ( ) ;
251+
252+ var credentials = new Credentials
253+ {
254+ Email = "manager@example.com" ,
255+ Password = "password123"
256+ } ;
257+
258+ _mockJwtUtils . Setup ( x => x . GenerateJwtToken ( It . IsAny < User > ( ) ) )
259+ . Returns ( "manager-jwt-token" ) ;
260+
261+ // Act
262+ var result = await _controller . Login ( credentials ) ;
263+
264+ // Assert
265+ Assert . That ( result . Result , Is . Null ) ;
266+ Assert . That ( result . Value , Is . Not . Null ) ;
267+ Assert . That ( result . Value , Is . TypeOf < UserViewItemWithJWT > ( ) ) ;
268+
269+ var userView = result . Value as UserViewItemWithJWT ;
270+ Assert . That ( userView ! . Token , Is . EqualTo ( "manager-jwt-token" ) ) ;
271+ Assert . That ( userView . Email , Is . EqualTo ( "manager@example.com" ) ) ;
272+ Assert . That ( userView . Roles , Contains . Item ( UserRoleConstants . AccountManager ) ) ;
273+ Assert . That ( userView . AccountIds , Is . EquivalentTo ( new [ ] { 1 , 2 } ) ) ;
274+ }
275+
276+ [ Test ]
277+ public async Task Login_EmptyAccountIds_WhenUserIsNotManager ( )
278+ {
279+ // Arrange
280+ var engineerRole = new Role { Id = 101 , RoleId = UserRoleConstants . InstallationEngineer , Name = "Engineer" } ;
281+ _dbContext . Roles . Add ( engineerRole ) ;
282+
283+ string hashedPassword = BCrypt . Net . BCrypt . HashPassword ( "password123" ) ;
284+ var engineerUser = new User
285+ {
286+ Id = 3 ,
287+ Email = "engineer@example.com" ,
288+ Password = hashedPassword ,
289+ FirstName = "Engineer" ,
290+ LastName = "User" ,
291+ Patronymic = "" ,
292+ UserRoles =
293+ [
294+ new UserRole { UserId = 3 , RoleId = engineerRole . Id , Role = engineerRole }
295+ ]
296+ } ;
297+
298+ _dbContext . Users . Add ( engineerUser ) ;
299+ await _dbContext . SaveChangesAsync ( ) ;
300+
301+ var credentials = new Credentials
302+ {
303+ Email = "engineer@example.com" ,
304+ Password = "password123"
305+ } ;
306+
307+ _mockJwtUtils . Setup ( x => x . GenerateJwtToken ( It . IsAny < User > ( ) ) )
308+ . Returns ( "engineer-jwt-token" ) ;
309+
310+ // Act
311+ var result = await _controller . Login ( credentials ) ;
312+
313+ // Assert
314+ Assert . That ( result . Result , Is . Null ) ;
315+ Assert . That ( result . Value , Is . Not . Null ) ;
316+ Assert . That ( result . Value , Is . TypeOf < UserViewItemWithJWT > ( ) ) ;
317+
318+ var userView = result . Value as UserViewItemWithJWT ;
319+ Assert . That ( userView ! . Token , Is . EqualTo ( "engineer-jwt-token" ) ) ;
320+ Assert . That ( userView . Email , Is . EqualTo ( "engineer@example.com" ) ) ;
321+ Assert . That ( userView . Roles , Contains . Item ( UserRoleConstants . InstallationEngineer ) ) ;
322+ Assert . That ( userView . AccountIds , Is . Empty ) ; // Non-managers should have empty AccountIds
323+ }
217324}
0 commit comments