|
| 1 | +using exercise.wwwapi.DTOs; |
| 2 | +using exercise.wwwapi.DTOs.Cohorts; |
| 3 | +using exercise.wwwapi.Endpoints; |
| 4 | +using exercise.wwwapi.Models; |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Linq; |
| 8 | +using System.Text; |
| 9 | +using System.Text.Json; |
| 10 | +using System.Threading.Tasks; |
| 11 | + |
| 12 | +namespace api.tests.CohortEndpointTests |
| 13 | +{ |
| 14 | + public class CreateCohortTests |
| 15 | + { |
| 16 | + private HttpClient _client; |
| 17 | + |
| 18 | + [SetUp] |
| 19 | + public void Setup() |
| 20 | + { |
| 21 | + _client = TestUtils.CreateClient(); |
| 22 | + } |
| 23 | + |
| 24 | + [TearDown] |
| 25 | + public void TearDown() |
| 26 | + { |
| 27 | + _client.Dispose(); |
| 28 | + } |
| 29 | + |
| 30 | + [Test] |
| 31 | + // Checks that creating a new cohort with valid values returns Created |
| 32 | + public async Task CreateCohortTest() |
| 33 | + { |
| 34 | + var newCohort = new CreateCohortRequestDTO |
| 35 | + { |
| 36 | + CohortNumber = 2, |
| 37 | + CohortName = "New Cohort", |
| 38 | + StartDate = DateTime.UtcNow, |
| 39 | + EndDate = DateTime.UtcNow.AddMonths(3) |
| 40 | + }; |
| 41 | + var content = new StringContent( |
| 42 | + JsonSerializer.Serialize(newCohort), |
| 43 | + System.Text.Encoding.UTF8, |
| 44 | + "application/json" |
| 45 | + ); |
| 46 | + var response = await _client.PostAsync("/cohorts", content); |
| 47 | + Assert.That(response.StatusCode, Is.EqualTo(System.Net.HttpStatusCode.Created)); |
| 48 | + } |
| 49 | + |
| 50 | + [Test] |
| 51 | + // Checks that creating a new cohort with invalid values returns BadRequest |
| 52 | + public async Task CreateCohortValidationFailsTest() |
| 53 | + { |
| 54 | + var newCohort = new CreateCohortRequestDTO |
| 55 | + { |
| 56 | + CohortNumber = -2, |
| 57 | + CohortName = "New Cohort", |
| 58 | + StartDate = DateTime.MinValue, |
| 59 | + EndDate = DateTime.MinValue |
| 60 | + }; |
| 61 | + var content = new StringContent( |
| 62 | + JsonSerializer.Serialize(newCohort), |
| 63 | + System.Text.Encoding.UTF8, |
| 64 | + "application/json" |
| 65 | + ); |
| 66 | + var response = await _client.PostAsync("/cohorts", content); |
| 67 | + Assert.That(response.StatusCode, Is.EqualTo(System.Net.HttpStatusCode.BadRequest)); |
| 68 | + } |
| 69 | + |
| 70 | + // Checks that creating a new cohort with missing required fields returns BadRequest |
| 71 | + [Test] |
| 72 | + public async Task CreateCohortMissingFieldsTest() |
| 73 | + { |
| 74 | + var newCohort = new |
| 75 | + { |
| 76 | + CohortNumber = 3, |
| 77 | + // CohortName is missing |
| 78 | + StartDate = DateTime.UtcNow, |
| 79 | + EndDate = DateTime.UtcNow.AddMonths(3) |
| 80 | + }; |
| 81 | + var content = new StringContent( |
| 82 | + JsonSerializer.Serialize(newCohort), |
| 83 | + System.Text.Encoding.UTF8, |
| 84 | + "application/json" |
| 85 | + ); |
| 86 | + var response = await _client.PostAsync("/cohorts", content); |
| 87 | + Assert.That(response.StatusCode, Is.EqualTo(System.Net.HttpStatusCode.BadRequest)); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments