Skip to content

Commit 1ca571b

Browse files
committed
Updated Accounts and Config Tests
- Added view model validation tests - Added update and change password tests
1 parent c1543c8 commit 1ca571b

File tree

3 files changed

+226
-2
lines changed

3 files changed

+226
-2
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using WebApi.Features.Config;
4+
5+
namespace Test.Api.ClassData
6+
{
7+
public class InvalidConfigModels : IEnumerable<object[]>
8+
{
9+
public IEnumerator<object[]> GetEnumerator()
10+
{
11+
12+
// error: Time In is required
13+
yield return new object[]
14+
{
15+
new ConfigViewModel
16+
{
17+
TimeIn = "",
18+
TimeOut = "17:00",
19+
GracePeriod = 10,
20+
}
21+
};
22+
23+
// error: Time Out is required
24+
yield return new object[]
25+
{
26+
new ConfigViewModel
27+
{
28+
TimeIn = "25:00",
29+
TimeOut = "",
30+
GracePeriod = 10,
31+
}
32+
};
33+
34+
// error: Invalid TimeIn
35+
yield return new object[]
36+
{
37+
new ConfigViewModel
38+
{
39+
TimeIn = "25:00",
40+
TimeOut = "17:00",
41+
GracePeriod = 10,
42+
}
43+
};
44+
45+
// error: Invalid TimeOut
46+
yield return new object[]
47+
{
48+
new ConfigViewModel
49+
{
50+
TimeIn = "09:00",
51+
TimeOut = "25:00",
52+
GracePeriod = 10,
53+
}
54+
};
55+
56+
// error: Invalid GracePeriod
57+
yield return new object[]
58+
{
59+
new ConfigViewModel
60+
{
61+
TimeIn = "09:00",
62+
TimeOut = "17:00",
63+
GracePeriod = 60,
64+
}
65+
};
66+
67+
// error: Invalid GracePeriod
68+
yield return new object[]
69+
{
70+
new ConfigViewModel
71+
{
72+
TimeIn = "09:00",
73+
TimeOut = "17:00",
74+
GracePeriod = -1,
75+
}
76+
};
77+
}
78+
79+
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
80+
}
81+
}

tests/Api/IntegrationTests/AccountsTest.cs

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using System.Linq;
22
using System.Net;
33
using System.Net.Http;
4+
using System.Text;
45
using System.Threading.Tasks;
56
using Microsoft.AspNetCore.Identity;
67
using Microsoft.EntityFrameworkCore;
8+
using Newtonsoft.Json;
79
using Test.Api.ClassData;
810
using Test.Api.Models;
911
using Test.Api.Utils;
@@ -84,5 +86,135 @@ public async Task GivenInvalidModels_WhenPostRegisterRequest_ThenReturnBadReques
8486
// Assert
8587
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
8688
}
89+
90+
[Fact]
91+
public async Task GivenEmployeeModel_WhenPutUpdatePasswordRequest_ThenReturnOk()
92+
{
93+
// Arrange
94+
const string NEW_PASSWORD = "654321";
95+
96+
var model = _fixture.Context.Users.ToList().ElementAt(5);
97+
var viewModel = new UpdatePasswordViewModel
98+
{
99+
UserName = model.UserName,
100+
Password = NEW_PASSWORD,
101+
};
102+
103+
// Act
104+
var response = await _fixture.Client.PutAsJsonAsync($"{API_URL}/update-password", viewModel);
105+
106+
// Assert
107+
response.EnsureSuccessStatusCode();
108+
109+
var assertLogin = await AuthExtensions.GetJwt(
110+
_fixture,
111+
new LoginViewModel
112+
{
113+
UserName = model.UserName,
114+
Password = NEW_PASSWORD,
115+
}
116+
);
117+
Assert.NotEmpty(assertLogin);
118+
}
119+
120+
[Theory]
121+
[InlineData("", "valid_password")]
122+
[InlineData("valid_username", "")]
123+
[InlineData("invalid_username", "valid_password")]
124+
// error: min length for password 6
125+
[InlineData("valid_username", "12345")]
126+
public async Task GivenInvalidViewModels_WhenPutUpdatePasswordRequest_ThenReturnBadRequest(string username, string password)
127+
{
128+
// Arrange
129+
var viewModel = new UpdatePasswordViewModel
130+
{
131+
UserName = username,
132+
Password = password,
133+
};
134+
135+
if (username == "valid_username")
136+
{
137+
var validUserName = _fixture.Context.Users.Last().UserName;
138+
viewModel.UserName = validUserName;
139+
}
140+
141+
// Act
142+
var response = await _fixture.Client.PutAsJsonAsync($"{API_URL}/update-password", viewModel);
143+
144+
// Assert
145+
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
146+
}
147+
148+
[Fact]
149+
public async Task GivenAuthByEmployee_WhenPutChangePasswordRequest_ThenReturnOk()
150+
{
151+
// Arrange
152+
const string NEW_PASSWORD = "654321";
153+
const string DEFAULT_PASSWORD = DefaultAdmin.Password;
154+
155+
var model = _fixture.Context.Users.Last();
156+
var viewModel = await Task.Run(() => JsonConvert.SerializeObject(
157+
new ChangePasswordViewModel
158+
{
159+
OldPassword = DEFAULT_PASSWORD,
160+
NewPassword = NEW_PASSWORD,
161+
}
162+
));
163+
164+
// Act
165+
var request = new HttpRequestMessage(HttpMethod.Put, $"{API_URL}/change-password");
166+
// Send request as Employee
167+
request.Content = new StringContent(viewModel, Encoding.UTF8, "application/json");
168+
169+
await AuthExtensions.SetupRequestAuth(
170+
request,
171+
_fixture,
172+
new LoginViewModel
173+
{
174+
UserName = model.UserName,
175+
Password = DEFAULT_PASSWORD,
176+
}
177+
);
178+
179+
var response = await _fixture.Client.SendAsync(request);
180+
181+
// Assert
182+
response.EnsureSuccessStatusCode();
183+
184+
var assertLogin = await AuthExtensions.GetJwt(
185+
_fixture,
186+
new LoginViewModel
187+
{
188+
UserName = model.UserName,
189+
Password = NEW_PASSWORD,
190+
}
191+
);
192+
Assert.NotEmpty(assertLogin);
193+
}
194+
195+
[Theory]
196+
[InlineData("", "valid_password")]
197+
[InlineData("valid_old_password", "")]
198+
[InlineData("invalid_old_password", "valid_password")]
199+
// error: min length for password 6
200+
[InlineData("valid_old_password", "12345")]
201+
public async Task GivenInvalidModels_WhenPutChangePasswordRequest_ThenReturnBadRequest(string oldPassword, string newPassword)
202+
{
203+
// Arrange
204+
var viewModel = new ChangePasswordViewModel
205+
{
206+
OldPassword = oldPassword,
207+
NewPassword = newPassword,
208+
};
209+
210+
if (oldPassword == "valid_old_password")
211+
viewModel.OldPassword = DefaultAdmin.Password;
212+
213+
// Act
214+
var response = await _fixture.Client.PutAsJsonAsync($"{API_URL}/change-password", viewModel);
215+
216+
// Assert
217+
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
218+
}
87219
}
88220
}

tests/Api/IntegrationTests/ConfigTest.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System.Linq;
2+
using System.Net;
23
using System.Net.Http;
34
using System.Threading.Tasks;
45
using Newtonsoft.Json;
6+
using Test.Api.ClassData;
57
using Test.Api.Utils;
68
using WebApi.Constants;
79
using WebApi.Features.Auth;
@@ -53,7 +55,7 @@ public async Task PutRequest_ThenReturnOk()
5355
var model = _fixture.Context.Config.First();
5456
model.TimeIn = "09:30";
5557
model.TimeOut = "17:15";
56-
model.GracePeriod = "5";
58+
model.GracePeriod = 5;
5759

5860
// Act
5961
var response = await _fixture.Client.PutAsJsonAsync(API_URL, model);
@@ -67,6 +69,15 @@ public async Task PutRequest_ThenReturnOk()
6769
Assert.Equal(updatedModel.GracePeriod, model.GracePeriod);
6870
}
6971

70-
// TODO: Add time validation in `ConfigViewModel`
72+
[Theory]
73+
[ClassData(typeof(InvalidConfigModels))]
74+
public async Task GivenInvalidConfigModels_WhenPutRequest_ThenReutrnBadRequest(ConfigViewModel viewModel)
75+
{
76+
// Act
77+
var response = await _fixture.Client.PutAsJsonAsync(API_URL, viewModel);
78+
79+
// Assert
80+
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
81+
}
7182
}
7283
}

0 commit comments

Comments
 (0)