Skip to content

Commit d4ef6c5

Browse files
committed
Merge branch 'main' into 106-backend---implement-getcohortbyuserid
2 parents afbe033 + 42b3a42 commit d4ef6c5

File tree

10 files changed

+182
-152
lines changed

10 files changed

+182
-152
lines changed

exercise.tests/IntegrationTests/PostTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public async Task GetAllPosts()
4444
using (Assert.EnterMultipleScope())
4545
{
4646
Assert.That(post?["id"]?.GetValue<int>(), Is.GreaterThan(0));
47-
Assert.That(post["content"]?.GetValue<string>(), Is.Not.Null);
47+
Assert.That(post?["content"]?.GetValue<string>(), Is.Not.Null);
4848
Assert.That(post["numLikes"]?.GetValue<int>(), Is.Not.Null);
4949
Assert.That(post["createdAt"], Is.Not.Null);
5050
}
@@ -569,7 +569,6 @@ public async Task GetPostsByUser_NotFound()
569569
[Test]
570570
public async Task GetCommentsByUser_Success()
571571
{
572-
573572
//Assert get token
574573
var token = await LoginAndGetToken("oyvind.perez1@example.com", "SuperHash!4");
575574

exercise.tests/IntegrationTests/UserTests.cs

Lines changed: 50 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,16 @@
66
using System.Text.Json;
77
using System.Text.Json.Nodes;
88
using exercise.tests.Helpers;
9+
using exercise.wwwapi.DTOs;
910

1011
namespace exercise.tests.IntegrationTests
1112
{
1213
/// <summary>
1314
/// Integration tests exercising the user management endpoints end-to-end via the API surface.
1415
/// </summary>
1516
[TestFixture]
16-
public class UserTests
17+
public class UserTests : BaseIntegrationTest
1718
{
18-
private WebApplicationFactory<Program> _factory;
19-
private HttpClient _client;
20-
21-
[SetUp]
22-
public void SetUp()
23-
{
24-
// Arrange
25-
_factory = new WebApplicationFactory<Program>();
26-
_client = _factory.CreateClient();
27-
}
28-
29-
[TearDown]
30-
public void TearDown()
31-
{
32-
_client.Dispose();
33-
_factory.Dispose();
34-
}
35-
3619
/// <summary>
3720
/// Confirms that valid registration payloads yield an HTTP 201 Created response.
3821
/// </summary>
@@ -193,19 +176,18 @@ public async Task Login_failure(string email, string password)
193176
[Test]
194177
public async Task UpdateUserSuccess()
195178
{
196-
var fieldsToUpdate = new Dictionary<string, object?>
179+
var fieldsToUpdate = new UserPatchDTO()
197180
{
198-
{ "username", "roman-olsen13"},
199-
{ "email", "roman.olsen13@example.com"},
200-
{ "password", "aGoodPassword!200" },
201-
{ "role", 0}
181+
Username = "roman-olsen13",
182+
Email = "roman.olsen13@example.com",
183+
Password = "aGoodPassword!200" ,
184+
Role = 0
202185
};
203186

204-
var json = JsonSerializer.Serialize(fieldsToUpdate);
205-
var content = new StringContent(json, Encoding.UTF8, "application/json");
206-
187+
string token = await LoginAndGetToken(TeacherEmail, TeacherPassword);
207188
int userId = 13;
208-
var response = await _client.PatchAsync($"/users/{userId}", content);
189+
var response = await SendAuthenticatedPatchAsync($"/users/{userId}", token, fieldsToUpdate);
190+
209191

210192
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
211193
}
@@ -216,14 +198,12 @@ public async Task UpdateUserSuccess()
216198
[Test]
217199
public async Task UpdateUserNullFieldsOnly()
218200
{
219-
var fieldsToUpdate = new Dictionary<string, object?> { };
220-
221-
var json = JsonSerializer.Serialize(fieldsToUpdate);
222-
var content = new StringContent(json, Encoding.UTF8, "application/json");
223-
201+
var fieldsToUpdate = new UserPatchDTO();
202+
203+
string token = await LoginAndGetToken(TeacherEmail, TeacherPassword);
224204
int userId = 1;
225-
var response = await _client.PatchAsync($"/users/{userId}", content);
226-
205+
206+
var response = await SendAuthenticatedPatchAsync($"/users/{userId}", token, fieldsToUpdate);
227207
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
228208
}
229209

@@ -233,17 +213,15 @@ public async Task UpdateUserNullFieldsOnly()
233213
[Test]
234214
public async Task UpdateUserInvalidUsername()
235215
{
236-
var fieldsToUpdate = new Dictionary<string, object?>
216+
var fieldsToUpdate = new UserPatchDTO()
237217
{
238-
{ "username", "roman--olsen13"}
218+
Username = "roman--olsen13"
239219
};
240220

241-
var json = JsonSerializer.Serialize(fieldsToUpdate);
242-
var content = new StringContent(json, Encoding.UTF8, "application/json");
243-
244221
int userId = 13;
245-
var response = await _client.PatchAsync($"/users/{userId}", content);
246-
222+
string token = await LoginAndGetToken(TeacherEmail, TeacherPassword);
223+
224+
var response = await SendAuthenticatedPatchAsync($"/users/{userId}", token, fieldsToUpdate);
247225
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
248226
}
249227

@@ -253,17 +231,15 @@ public async Task UpdateUserInvalidUsername()
253231
[Test]
254232
public async Task UpdateUserInvalidGitHubUsername()
255233
{
256-
var fieldsToUpdate = new Dictionary<string, object?>
234+
var fieldsToUpdate = new UserPatchDTO()
257235
{
258-
{ "gitHubUsername", "roman--olsen13"}
236+
GithubUsername = "roman--olsen13"
259237
};
260238

261-
var json = JsonSerializer.Serialize(fieldsToUpdate);
262-
var content = new StringContent(json, Encoding.UTF8, "application/json");
263-
264239
int userId = 13;
265-
var response = await _client.PatchAsync($"/users/{userId}", content);
266-
240+
string token = await LoginAndGetToken(TeacherEmail, TeacherPassword);
241+
242+
var response = await SendAuthenticatedPatchAsync($"/users/{userId}", token, fieldsToUpdate);
267243
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
268244
}
269245

@@ -273,16 +249,15 @@ public async Task UpdateUserInvalidGitHubUsername()
273249
[Test]
274250
public async Task UpdateUserInvalidEmail()
275251
{
276-
var fieldsToUpdate = new Dictionary<string, object?>
252+
var fieldsToUpdate = new UserPatchDTO()
277253
{
278-
{ "email", "roman.olsen13@.e.com"}
254+
Email = "roman.olsen13@.e.com"
279255
};
280256

281-
var json = JsonSerializer.Serialize(fieldsToUpdate);
282-
var content = new StringContent(json, Encoding.UTF8, "application/json");
257+
var token = await LoginAndGetToken(TeacherEmail, TeacherPassword);
283258

284259
int userId = 13;
285-
var response = await _client.PatchAsync($"/users/{userId}", content);
260+
var response = await SendAuthenticatedPatchAsync($"/users/{userId}", token, fieldsToUpdate);
286261

287262
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
288263
}
@@ -298,12 +273,11 @@ public async Task UpdateUserInvalidPassword()
298273
{ "password", "nope!"}
299274
};
300275

301-
var json = JsonSerializer.Serialize(fieldsToUpdate);
302-
var content = new StringContent(json, Encoding.UTF8, "application/json");
276+
var token = await LoginAndGetToken(TeacherEmail, TeacherPassword);
303277

304278
int userId = 13;
305-
var response = await _client.PatchAsync($"/users/{userId}", content);
306-
279+
var response = await SendAuthenticatedPatchAsync($"/users/{userId}", token, fieldsToUpdate);
280+
307281
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
308282
}
309283

@@ -318,11 +292,10 @@ public async Task UpdateUserInvalidRole()
318292
{ "role", 10}
319293
};
320294

321-
var json = JsonSerializer.Serialize(fieldsToUpdate);
322-
var content = new StringContent(json, Encoding.UTF8, "application/json");
295+
var token = await LoginAndGetToken(TeacherEmail, TeacherPassword);
323296

324297
int userId = 13;
325-
var response = await _client.PatchAsync($"/users/{userId}", content);
298+
var response = await SendAuthenticatedPatchAsync($"/users/{userId}", token, fieldsToUpdate);
326299

327300
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
328301
}
@@ -338,11 +311,10 @@ public async Task UpdateUserUsernameExists()
338311
{ "username", "nigel-nowak2"}
339312
};
340313

341-
var json = JsonSerializer.Serialize(fieldsToUpdate);
342-
var content = new StringContent(json, Encoding.UTF8, "application/json");
314+
var token = await LoginAndGetToken(TeacherEmail, TeacherPassword);
343315

344316
int userId = 13;
345-
var response = await _client.PatchAsync($"/users/{userId}", content);
317+
var response = await SendAuthenticatedPatchAsync($"/users/{userId}", token, fieldsToUpdate);
346318

347319
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
348320
}
@@ -353,16 +325,15 @@ public async Task UpdateUserUsernameExists()
353325
[Test]
354326
public async Task UpdateUserGitHubUsernameExists()
355327
{
356-
var fieldsToUpdate = new Dictionary<string, object?>
328+
var fieldsToUpdate = new UserPatchDTO()
357329
{
358-
{ "gitHubUsername", "nigel-nowak2"}
330+
GithubUsername = "nigel-nowak2"
359331
};
360332

361-
var json = JsonSerializer.Serialize(fieldsToUpdate);
362-
var content = new StringContent(json, Encoding.UTF8, "application/json");
363-
364333
int userId = 13;
365-
var response = await _client.PatchAsync($"/users/{userId}", content);
334+
string token = await LoginAndGetToken(TeacherEmail, TeacherPassword);
335+
336+
var response = await SendAuthenticatedPatchAsync($"/users/{userId}", token, fieldsToUpdate);
366337

367338
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
368339
}
@@ -373,16 +344,15 @@ public async Task UpdateUserGitHubUsernameExists()
373344
[Test]
374345
public async Task UpdateUserEmailExists()
375346
{
376-
var fieldsToUpdate = new Dictionary<string, object?>
347+
var fieldsToUpdate = new UserPatchDTO()
377348
{
378-
{ "email", "nigel.nowak2@example.com"}
349+
Email="nigel.nowak2@example.com"
379350
};
380-
381-
var json = JsonSerializer.Serialize(fieldsToUpdate);
382-
var content = new StringContent(json, Encoding.UTF8, "application/json");
383-
351+
384352
int userId = 13;
385-
var response = await _client.PatchAsync($"/users/{userId}", content);
353+
string token = await LoginAndGetToken(TeacherEmail, TeacherPassword);
354+
355+
var response = await SendAuthenticatedPatchAsync($"/users/{userId}", token, fieldsToUpdate);
386356

387357
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
388358
}
@@ -396,7 +366,9 @@ public async Task UpdateUserEmailExists()
396366
[TestCase("10000000", HttpStatusCode.NotFound)]
397367
public async Task GetUserByIdTest(string id, HttpStatusCode responseStatus)
398368
{
399-
var response = await _client.GetAsync($"/users/{id}");
369+
string token = await LoginAndGetToken(TeacherEmail, TeacherPassword);
370+
371+
var response = await SendAuthenticatedGetAsync($"/users/{id}", token);
400372
Assert.That(response.StatusCode, Is.EqualTo(responseStatus));
401373

402374
}

exercise.wwwapi/DTOs/Cohort/CourseInCohortDTO.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ namespace exercise.wwwapi.DTOs.Cohort
55
public class CourseInCohortDTO
66
{
77
public string Title { get; set; }
8+
public int Id { get; set; }
9+
810
public ICollection<UserBasicDTO> Students { get; set; } = new List<UserBasicDTO>();
911
public ICollection<UserBasicDTO> Teachers { get; set; } = new List<UserBasicDTO>();
1012
//public ICollection<UserCohortDTO> Students { get; set; } = new List<UserCohortDTO>();

exercise.wwwapi/Endpoints/CohortEndpoints.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ public static void ConfigureCohortEndpoints(this WebApplication app)
1818
var cohorts = app.MapGroup("cohorts");
1919
cohorts.MapPost("/", CreateCohort).WithSummary("Create a cohort");
2020
cohorts.MapGet("/", GetAllCohorts).WithSummary("Get all cohorts");
21-
cohorts.MapGet("/cohortId/{id}", GetCohort).WithSummary("Get a cohort by ID");
21+
cohorts.MapGet("/cohortId/{cohortId}", GetCohort).WithSummary("Get a cohort by ID");
2222
cohorts.MapGet("/userId/{userId}", GetCohortByUserId).WithSummary("Get all cohorts a user is in by its Id");
23-
cohorts.MapPost("/{cohortId}/{userId}/{courseId}", AddUserToCohort).WithSummary("Add a user to a cohort");
24-
cohorts.MapDelete("/{cohortId}/{userId}/{courseId}", DeleteUserFromCohort).WithSummary("Delete a user from a cohort");
23+
cohorts.MapPost("/cohortId/{cohortId}/userId/{userId}/courseId/{courseId}", AddUserToCohort).WithSummary("Add a user to a cohort");
24+
cohorts.MapDelete("/cohortId/{cohortId}/userId/{userId}/courseId/{courseId}", DeleteUserFromCohort).WithSummary("Delete a user from a cohort");
2525
}
2626

2727
[Authorize]

exercise.wwwapi/Endpoints/LogEndpoints.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Microsoft.AspNetCore.Mvc;
1+
using Microsoft.AspNetCore.Authorization;
2+
using Microsoft.AspNetCore.Mvc;
23

34
namespace exercise.wwwapi.Endpoints
45
{
@@ -10,10 +11,12 @@ public static void ConfigureLogEndpoints(this WebApplication app)
1011
logs.MapPost("/", CreateDeliveryLog).WithSummary("Create a delivery log");
1112
}
1213

14+
[Authorize]
1315
[ProducesResponseType(StatusCodes.Status200OK)]
14-
public static async Task<IResult> CreateDeliveryLog()
16+
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
17+
private static Task<IResult> CreateDeliveryLog()
1518
{
16-
return TypedResults.Ok();
19+
return Task.FromResult<IResult>(TypedResults.Ok());
1720
}
1821
}
1922
}

0 commit comments

Comments
 (0)