1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Linq ;
4+ using System . Net ;
5+ using System . Net . Http ;
6+ using System . Threading . Tasks ;
7+ using Microsoft . EntityFrameworkCore ;
8+ using Newtonsoft . Json ;
9+ using Test . Api . Models ;
10+ using Test . Api . Utils ;
11+ using WebApi . Constants ;
12+ using WebApi . Features . Auth ;
13+ using WebApi . Features . Employees ;
14+ using Xunit ;
15+
16+ namespace Test . Api
17+ {
18+ public class EmployeeTest : IClassFixture < TestServerFixture >
19+ {
20+ private const string API_URL = "api/employee" ;
21+ private readonly TestServerFixture _fixture ;
22+
23+ public EmployeeTest ( TestServerFixture fixture )
24+ {
25+ _fixture = fixture ;
26+
27+ // Set up Default Auth as Admin
28+ var userCredentials = new LoginViewModel
29+ {
30+ UserName = DefaultAdmin . UserName ,
31+ Password = DefaultAdmin . Password
32+ } ;
33+ AuthExtensions . SetupJwtAuth ( _fixture , userCredentials ) . Wait ( ) ;
34+ }
35+
36+ [ Fact ]
37+ public async Task GetList_ThenReturnOk ( )
38+ {
39+ // Arrange
40+ var totalEmploeeFromDb = _fixture . Context . Employees . Count ( ) ;
41+
42+ // Act
43+ var response = await _fixture . Client . GetAsync ( API_URL ) ;
44+ var result = await response . Content . ReadAsStringAsync ( ) ;
45+ var resultModel = JsonConvert . DeserializeObject < ICollection < EmployeeViewModel > > ( result ) ;
46+
47+ // Assert
48+ response . EnsureSuccessStatusCode ( ) ;
49+
50+ var resultCount = resultModel . Count ( ) ;
51+ Assert . Equal ( totalEmploeeFromDb , resultCount ) ;
52+ }
53+
54+ [ Fact ]
55+ public async Task GetDetails_ThenReturnOk ( )
56+ {
57+ // Arrange
58+ var model = _fixture . Context . Employees . First ( ) ;
59+ var employeeId = model . Id ;
60+
61+ // Act
62+ var response = await _fixture . Client . GetAsync ( $ "{ API_URL } /{ employeeId } ") ;
63+ var result = await response . Content . ReadAsStringAsync ( ) ;
64+ var resultModel = JsonConvert . DeserializeObject < EmployeeViewModel > ( result ) ;
65+
66+ // Assert
67+ response . EnsureSuccessStatusCode ( ) ;
68+ Assert . Equal ( model . FullName , resultModel . FullName ) ;
69+ Assert . Equal ( model . Position , resultModel . Position ) ;
70+ Assert . Equal ( model . CardNo , resultModel . CardNo ) ;
71+ Assert . Equal ( model . Status , resultModel . Status ) ;
72+ Assert . Equal ( model . IdentityId , resultModel . IdentityId ) ;
73+ }
74+
75+ [ Fact ]
76+ public async Task GivenValidEmployee_WhenPutRequest_ThenReturnOkWithUpdatedModel ( )
77+ {
78+ // Arrange
79+ var testModel = new EmployeeTestModel ( ) ;
80+ var model = _fixture . Context . Employees . Last ( ) ;
81+ model . FullName = testModel . FullName ;
82+ model . CardNo = testModel . CardNo ;
83+ model . Position = testModel . Position ;
84+
85+ // Act
86+ var response = await _fixture . Client . PutAsJsonAsync ( API_URL , model ) ;
87+
88+ // Assert
89+ response . EnsureSuccessStatusCode ( ) ;
90+
91+ var updatedModel = _fixture . Context . Employees . Find ( model . Id ) ;
92+ Assert . Equal ( model , updatedModel ) ;
93+ }
94+
95+ [ Theory ]
96+ [ InlineData ( "" , "full_name" , "card_no" ) ]
97+ [ InlineData ( "718cae62-3856-4620-bada-321cc66023f0" , "" , "card_no" ) ]
98+ [ InlineData ( "718cae62-3856-4620-bada-321cc66023f0" , "full_name" , "" ) ]
99+ [ InlineData ( "718cae62-3856-4620-bada-321cc66023f0" , "full_name" , "existing_card_no" ) ]
100+ public async Task GivenInvalidEmployee_WhenPutRequest_ThenReturnBadRequest ( string identityId , string fullName , string cardNo )
101+ {
102+ // Arrange
103+ var testModel = new EmployeeTestModel ( ) ;
104+ var model = _fixture . Context . Employees . Last ( ) ;
105+
106+ model . IdentityId = identityId ?? model . IdentityId ;
107+ model . FullName = fullName ?? model . FullName ;
108+ model . CardNo = cardNo ?? model . CardNo ;
109+
110+ if ( cardNo == "existing_card_no" )
111+ {
112+ var existingCardNo = _fixture . Context . Employees . First ( ) . CardNo ;
113+ model . CardNo = existingCardNo ;
114+ }
115+
116+ // Act
117+ var response = await _fixture . Client . PutAsJsonAsync ( API_URL , model ) ;
118+
119+ // Assert
120+ Assert . Equal ( HttpStatusCode . BadRequest , response . StatusCode ) ;
121+ }
122+ }
123+ }
0 commit comments