Skip to content

Commit af44642

Browse files
author
Roman Eriksen
committed
Merge branch 'main' into 91-backend---implement-cohort-structure
2 parents 6a52833 + 9e03ba6 commit af44642

33 files changed

+285
-160
lines changed

exercise.tests/Helpers/UserTestCases.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
1-
using NUnit.Framework;
2-
using System.Collections.Generic;
3-
41
namespace exercise.tests.Helpers
52
{
3+
/// <summary>
4+
/// Provides reusable user-centric <see cref="TestCaseData"/> scenarios for integration tests.
5+
/// </summary>
66
public static class UserTestCases
77
{
8+
/// <summary>
9+
/// Supplies registration payloads that should produce successful responses.
10+
/// </summary>
811
public static IEnumerable<TestCaseData> ValidRegisterCases()
912
{
1013
yield return new TestCaseData("validuser", "valid@email.com", "ValidPass1!");
1114
yield return new TestCaseData("user-name", "user1@example.com", "SecurePass9#");
1215
yield return new TestCaseData("user1name", "user2@example.com", "StrongPass$1");
1316
}
1417

18+
/// <summary>
19+
/// Supplies registration payloads that should fail validation.
20+
/// </summary>
1521
public static IEnumerable<TestCaseData> InvalidRegisterCases()
1622
{
1723
yield return new TestCaseData("validuser", "plainaddress", "ValidPass1!").SetName("Invalid: Invalid email format (register)");
@@ -22,6 +28,9 @@ public static IEnumerable<TestCaseData> InvalidRegisterCases()
2228
yield return new TestCaseData("validuser", "valid@email.com", "NoSpecialChar1").SetName("Invalid: Missing special character (register)");
2329
}
2430

31+
/// <summary>
32+
/// Supplies credential pairs that represent successful login attempts.
33+
/// </summary>
2534
public static IEnumerable<TestCaseData> ValidLoginCases()
2635
{
2736
yield return new TestCaseData("oyvind.perez1@example.com", "SuperHash!4");
@@ -30,6 +39,9 @@ public static IEnumerable<TestCaseData> ValidLoginCases()
3039
//yield return new TestCaseData("nigel.nowak2@example.com", "ValidPass1!");
3140
}
3241

42+
/// <summary>
43+
/// Supplies credential pairs that should fail authentication.
44+
/// </summary>
3345
public static IEnumerable<TestCaseData> InvalidLoginCases()
3446
{
3547
yield return new TestCaseData("valid@email.com", "short1!").SetName("Invalid: Password too short (login)");
@@ -40,4 +52,4 @@ public static IEnumerable<TestCaseData> InvalidLoginCases()
4052
yield return new TestCaseData("user@domain.c", "ValidPass1!").SetName("Invalid: Invalid email domain (login)");
4153
}
4254
}
43-
}
55+
}

exercise.tests/Helpers/UsernameValidationCases.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
using NUnit.Framework;
2-
using System;
3-
using System.Collections.Generic;
4-
using System.Net;
1+
using System.Net;
52

63
namespace exercise.tests.Helpers
74
{
5+
/// <summary>
6+
/// Centralises username validation scenarios for reuse across integration tests.
7+
/// </summary>
88
public static class UsernameValidationTestData
99
{
10+
/// <summary>
11+
/// Yields username samples alongside the expected message outcome for each endpoint.
12+
/// </summary>
1013
public static IEnumerable<TestCaseData> UsernameValidationMessageCases()
11-
{
14+
{
1215
string[] endpoints = new string[]
1316
{
1417
"username",
@@ -56,6 +59,9 @@ public static IEnumerable<TestCaseData> UsernameValidationMessageCases()
5659
}
5760

5861
}
62+
/// <summary>
63+
/// Yields username samples alongside the expected status code for each endpoint.
64+
/// </summary>
5965
public static IEnumerable<TestCaseData> UsernameValidationStatusCases()
6066
{
6167
string[] endpoints = new string[]
@@ -103,6 +109,6 @@ public static IEnumerable<TestCaseData> UsernameValidationStatusCases()
103109
}
104110
}
105111
}
106-
112+
107113
}
108114
}

exercise.tests/IntegrationTests/EndpointStatusTest.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
using exercise.wwwapi.DTOs.Login;
2-
using exercise.wwwapi.DTOs.Register;
3-
using Microsoft.AspNetCore.Http;
4-
using Microsoft.AspNetCore.Mvc.Testing;
1+
using Microsoft.AspNetCore.Mvc.Testing;
52
using System.Net;
63
using System.Text;
74
using System.Text.Json;
85
using System.Text.Json.Nodes;
96

107
namespace exercise.tests.IntegrationTests
118
{
9+
/// <summary>
10+
/// Integration smoke tests that ensure unexpected endpoints return consistent error responses.
11+
/// </summary>
1212
[TestFixture]
1313
public class EndpointStatusTest
1414
{
@@ -30,11 +30,15 @@ public void TearDown()
3030
_factory.Dispose();
3131
}
3232

33-
[Test]
33+
/// <summary>
34+
/// Verifies POST requests to unknown routes return a 404 with the canonical payload.
35+
/// </summary>
36+
[Test]
3437
public async Task PostCheckInvalidEndpoint()
3538
{
36-
var body = new {
37-
email= $"myemailVery@gmail.com",
39+
var body = new
40+
{
41+
email = $"myemailVery@gmail.com",
3842
password = "someR21!password"
3943
};
4044
var json = JsonSerializer.Serialize(body);
@@ -59,6 +63,9 @@ public async Task PostCheckInvalidEndpoint()
5963
Assert.That(message?["message"]?.GetValue<string>(), Is.EqualTo("Endpoint not found"));
6064
}
6165

66+
/// <summary>
67+
/// Verifies GET requests to unknown routes return a 404 with the canonical payload.
68+
/// </summary>
6269
[Test]
6370
public async Task GetCheckInvalidEndpoint()
6471
{

exercise.tests/IntegrationTests/PostTests.cs

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
using exercise.wwwapi.DTOs.Posts;
2-
using Microsoft.AspNetCore.Http;
32
using Microsoft.AspNetCore.Mvc.Testing;
4-
using System;
5-
using System.Collections.Generic;
6-
using System.Linq;
73
using System.Net;
84
using System.Text;
95
using System.Text.Json;
106
using System.Text.Json.Nodes;
11-
using System.Threading.Tasks;
127

138
namespace exercise.tests.IntegrationTests
149
{
10+
/// <summary>
11+
/// Integration tests covering the lifecycle of posts via the public API endpoints.
12+
/// </summary>
1513
[TestFixture]
1614
public class PostTests
1715
{
@@ -33,6 +31,9 @@ public void TearDown()
3331
_factory.Dispose();
3432
}
3533

34+
/// <summary>
35+
/// Ensures GET /posts returns a successful response and the expected payload structure.
36+
/// </summary>
3637
[Test]
3738
public async Task GetAllPosts()
3839
{
@@ -82,6 +83,9 @@ public async Task GetAllPosts()
8283
}
8384
}
8485

86+
/// <summary>
87+
/// Confirms creating a post with valid data yields an HTTP 201 Created status.
88+
/// </summary>
8589
[Test]
8690
public async Task SuccessfulCreatePostStatus()
8791
{
@@ -111,6 +115,9 @@ public async Task SuccessfulCreatePostStatus()
111115
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.Created));
112116
}
113117

118+
/// <summary>
119+
/// Validates the create post response body mirrors the request payload and contracts.
120+
/// </summary>
114121
[Test]
115122
public async Task SuccessfulCreatePostMessage()
116123
{
@@ -161,6 +168,12 @@ public async Task SuccessfulCreatePostMessage()
161168
Assert.That(data["createdAt"], Is.Not.Null);
162169
}
163170

171+
/// <summary>
172+
/// Checks that invalid create post requests surface the correct HTTP status codes.
173+
/// </summary>
174+
/// <param name="userid">The author identifier to include in the payload.</param>
175+
/// <param name="content">The post body content being validated.</param>
176+
/// <param name="expected">The status code the API should return.</param>
164177
[TestCase(9999999, "somecontent", HttpStatusCode.NotFound)]
165178
[TestCase(5, "", HttpStatusCode.BadRequest)]
166179
[TestCase(5, " ", HttpStatusCode.BadRequest)]
@@ -191,6 +204,12 @@ public async Task FailedlCreatePostStatus(int userid, string content, HttpStatus
191204
Assert.That(response.StatusCode, Is.EqualTo(expected));
192205
}
193206

207+
/// <summary>
208+
/// Ensures the validation error message matches expectations for rejected create post attempts.
209+
/// </summary>
210+
/// <param name="userid">The author identifier placed in the request.</param>
211+
/// <param name="content">The invalid content variation under test.</param>
212+
/// <param name="expectedmessage">The message the API should return.</param>
194213
[TestCase(9999999, "somecontent", "Invalid userID")]
195214
[TestCase(5, "", "Content cannot be empty")]
196215
[TestCase(5, " ", "Content cannot be empty")]
@@ -224,6 +243,9 @@ public async Task FailedCreatePostMessage(int userid, string content, string exp
224243
Assert.That(message["data"], Is.Null);
225244
Assert.That(message["timestamp"], Is.Not.Null);
226245
}
246+
/// <summary>
247+
/// Validates that deleting an existing post succeeds and a second deletion reports not found.
248+
/// </summary>
227249
[Test]
228250
public async Task DeletePostById_SuccessAndNotFound()
229251
{
@@ -262,6 +284,12 @@ public async Task DeletePostById_SuccessAndNotFound()
262284

263285

264286

287+
/// <summary>
288+
/// Exercises the PATCH /posts/{id} endpoint across happy path and error scenarios.
289+
/// </summary>
290+
/// <param name="postId">The identifier of the post to update.</param>
291+
/// <param name="newContent">The updated content supplied.</param>
292+
/// <param name="expected">The anticipated HTTP status code.</param>
265293
[TestCase(5, "Updated content", HttpStatusCode.OK)]
266294
[TestCase(9999999, "Updated content", HttpStatusCode.NotFound)]
267295
[TestCase(5, "", HttpStatusCode.BadRequest)]

0 commit comments

Comments
 (0)