Skip to content

Commit 14b5fdb

Browse files
author
Oyvind Timian Dokk Husveg
committed
Making tests
1 parent 8bec281 commit 14b5fdb

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

exercise.tests/IntegrationTests/BaseIntegrationTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ FROM users u
7272
protected const int StudentCommentID2 = 3;
7373

7474

75-
protected async Task<string> LoginAndGetToken(string email, string password, bool success = true)
75+
protected async Task<string> LoginAndGetToken(string email, string password, bool success = true, bool longlife = false)
7676
{
7777
var loginBody = new LoginRequestDTO { email = email, password = password };
7878
var loginRequestBody = new StringContent(
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using Microsoft.Extensions.Configuration;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.IdentityModel.Tokens.Jwt;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace exercise.tests.IntegrationTests
10+
{
11+
public class TokenTests : BaseIntegrationTest
12+
{
13+
[Test]
14+
public async Task CreateToken_ShouldGenerateValidJwt()
15+
{
16+
string token = await LoginAndGetToken(TeacherEmail, TeacherPassword);
17+
var handler = new JwtSecurityTokenHandler();
18+
var jwt = handler.ReadJwtToken(token);
19+
20+
Console.WriteLine(jwt);
21+
}
22+
23+
[Test]
24+
public async Task CreateToken_LongLife_ShouldExpireLater() {
25+
string token = await LoginAndGetToken(TeacherEmail, TeacherPassword, true, true);
26+
}
27+
28+
[Test]
29+
public async Task CreateToken_NormalLife_ShouldExpireLater()
30+
{
31+
string token = await LoginAndGetToken(TeacherEmail, TeacherPassword, true, true);
32+
}
33+
34+
}
35+
}

exercise.wwwapi/Endpoints/UserEndpoints.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,9 @@ private static IResult Login(IRepository<User> repository, IMapper mapper, Login
144144

145145
string token;
146146

147-
token = CreateToken(user, config);
148-
//if (request.longlifetoken.GetValueOrDefault()) token = CreateToken(user, config, 7);
149-
//else token = CreateToken(user, config, );
147+
//token = CreateToken(user, config);
148+
if (request.longlifetoken.GetValueOrDefault()) token = CreateToken(user, config, 7);
149+
else token = CreateToken(user, config, 0.0416666666666666666666666666666666666667);
150150

151151

152152
ResponseDTO <LoginSuccessDTO> response = new ResponseDTO<LoginSuccessDTO>
@@ -271,7 +271,7 @@ private static async Task<IResult> UpdateUser(IRepository<User> repository, Clai
271271
}
272272

273273
// Helper, creates jwt tokens
274-
private static string CreateToken(User user, IConfigurationSettings config)
274+
private static string CreateToken(User user, IConfigurationSettings config, double days)
275275
{
276276
List<Claim> claims =
277277
[
@@ -286,7 +286,7 @@ private static string CreateToken(User user, IConfigurationSettings config)
286286
var credentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha512Signature);
287287
var token = new JwtSecurityToken(
288288
claims: claims,
289-
expires: DateTime.Now.AddDays(1),
289+
expires: DateTime.Now.AddDays(days),
290290
signingCredentials: credentials
291291
);
292292
var jwt = new JwtSecurityTokenHandler().WriteToken(token);

0 commit comments

Comments
 (0)