|
| 1 | +using Definitions.Database; |
| 2 | +using Definitions.DTO.Identity; |
| 3 | +using Definitions.Web; |
| 4 | +using NUnit.Framework; |
| 5 | +using System.Net; |
| 6 | +using System.Net.Http.Json; |
| 7 | + |
| 8 | +namespace ObjectService.Tests.Integration; |
| 9 | + |
| 10 | +[TestFixture] |
| 11 | +public class IdentityRoutesTest : BaseRouteHandlerTestFixture |
| 12 | +{ |
| 13 | + public override string BaseRoute => string.Empty; |
| 14 | + |
| 15 | + protected override Task SeedDataCoreAsync(LocoDbContext db) |
| 16 | + { |
| 17 | + // No seed data needed for identity tests |
| 18 | + return Task.CompletedTask; |
| 19 | + } |
| 20 | + |
| 21 | + [Test] |
| 22 | + [Ignore("Not applicable for identity endpoints")] |
| 23 | + public override async Task ListAsync() |
| 24 | + { |
| 25 | + // Not applicable for identity endpoints |
| 26 | + await Task.CompletedTask; |
| 27 | + } |
| 28 | + |
| 29 | + [Test] |
| 30 | + [Ignore("Not applicable for identity endpoints")] |
| 31 | + public override async Task PostAsync() |
| 32 | + { |
| 33 | + // Not applicable for identity endpoints |
| 34 | + await Task.CompletedTask; |
| 35 | + } |
| 36 | + |
| 37 | + [Test] |
| 38 | + [Ignore("Not applicable for identity endpoints")] |
| 39 | + public override async Task GetAsync() |
| 40 | + { |
| 41 | + // Not applicable for identity endpoints |
| 42 | + await Task.CompletedTask; |
| 43 | + } |
| 44 | + |
| 45 | + [Test] |
| 46 | + [Ignore("Not applicable for identity endpoints")] |
| 47 | + public override async Task PutAsync() |
| 48 | + { |
| 49 | + // Not applicable for identity endpoints |
| 50 | + await Task.CompletedTask; |
| 51 | + } |
| 52 | + |
| 53 | + [Test] |
| 54 | + [Ignore("Not applicable for identity endpoints")] |
| 55 | + public override async Task DeleteAsync() |
| 56 | + { |
| 57 | + // Not applicable for identity endpoints |
| 58 | + await Task.CompletedTask; |
| 59 | + } |
| 60 | + |
| 61 | + [Test] |
| 62 | + public async Task Register_ShouldSucceed() |
| 63 | + { |
| 64 | + // arrange |
| 65 | + var registerRequest = new DtoRegisterRequest( |
| 66 | + Email: "test@example.com", |
| 67 | + UserName: "testuser", |
| 68 | + Password: "TestPassword123!" |
| 69 | + ); |
| 70 | + |
| 71 | + // act |
| 72 | + var response = await HttpClient!.PostAsJsonAsync("/register", registerRequest); |
| 73 | + |
| 74 | + // assert |
| 75 | + Assert.That(response.IsSuccessStatusCode, Is.True); |
| 76 | + } |
| 77 | + |
| 78 | + [Test] |
| 79 | + public async Task Register_WithInvalidEmail_ShouldFail() |
| 80 | + { |
| 81 | + // arrange |
| 82 | + var registerRequest = new DtoRegisterRequest( |
| 83 | + Email: "invalid-email", |
| 84 | + UserName: "testuser", |
| 85 | + Password: "TestPassword123!" |
| 86 | + ); |
| 87 | + |
| 88 | + // act |
| 89 | + var response = await HttpClient!.PostAsJsonAsync("/register", registerRequest); |
| 90 | + |
| 91 | + // assert |
| 92 | + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); |
| 93 | + } |
| 94 | + |
| 95 | + [Test] |
| 96 | + public async Task Login_WithValidCredentials_ShouldSucceed() |
| 97 | + { |
| 98 | + // arrange - First register a user |
| 99 | + var registerRequest = new DtoRegisterRequest( |
| 100 | + Email: "login@example.com", |
| 101 | + UserName: "loginuser", |
| 102 | + Password: "TestPassword123!" |
| 103 | + ); |
| 104 | + _ = await HttpClient!.PostAsJsonAsync("/register", registerRequest); |
| 105 | + |
| 106 | + var loginRequest = new |
| 107 | + { |
| 108 | + Email = "login@example.com", |
| 109 | + Password = "TestPassword123!" |
| 110 | + }; |
| 111 | + |
| 112 | + // act |
| 113 | + var response = await HttpClient!.PostAsJsonAsync("/login?useCookies=false", loginRequest); |
| 114 | + |
| 115 | + // assert |
| 116 | + Assert.That(response.IsSuccessStatusCode, Is.True); |
| 117 | + var result = await response.Content.ReadAsStringAsync(); |
| 118 | + Assert.That(result, Is.Not.Empty); |
| 119 | + } |
| 120 | + |
| 121 | + [Test] |
| 122 | + public async Task Login_WithInvalidCredentials_ShouldFail() |
| 123 | + { |
| 124 | + // arrange |
| 125 | + var loginRequest = new |
| 126 | + { |
| 127 | + Email = "nonexistent@example.com", |
| 128 | + Password = "WrongPassword123!" |
| 129 | + }; |
| 130 | + |
| 131 | + // act |
| 132 | + var response = await HttpClient!.PostAsJsonAsync("/login?useCookies=false", loginRequest); |
| 133 | + |
| 134 | + // assert |
| 135 | + Assert.That(response.IsSuccessStatusCode, Is.False); |
| 136 | + } |
| 137 | + |
| 138 | + [Test] |
| 139 | + public async Task Users_WithoutAuthentication_ShouldReturnUnauthorized() |
| 140 | + { |
| 141 | + // act |
| 142 | + var response = await HttpClient!.GetAsync($"{RoutesV2.Prefix}{RoutesV2.Users}"); |
| 143 | + |
| 144 | + // assert |
| 145 | + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.Unauthorized)); |
| 146 | + } |
| 147 | + |
| 148 | + [Test] |
| 149 | + public async Task Roles_WithoutAuthentication_ShouldReturnUnauthorized() |
| 150 | + { |
| 151 | + // act |
| 152 | + var response = await HttpClient!.GetAsync($"{RoutesV2.Prefix}{RoutesV2.Roles}"); |
| 153 | + |
| 154 | + // assert |
| 155 | + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.Unauthorized)); |
| 156 | + } |
| 157 | + |
| 158 | + [Test] |
| 159 | + public async Task Users_WithAuthentication_ShouldSucceed() |
| 160 | + { |
| 161 | + // arrange - Register a user |
| 162 | + var registerRequest = new DtoRegisterRequest( |
| 163 | + Email: "authtest@example.com", |
| 164 | + UserName: "authuser", |
| 165 | + Password: "TestPassword123!" |
| 166 | + ); |
| 167 | + _ = await HttpClient!.PostAsJsonAsync("/register", registerRequest); |
| 168 | + |
| 169 | + // Login to get bearer token |
| 170 | + var loginRequest = new |
| 171 | + { |
| 172 | + Email = "authtest@example.com", |
| 173 | + Password = "TestPassword123!" |
| 174 | + }; |
| 175 | + var loginResponse = await HttpClient!.PostAsJsonAsync("/login?useCookies=false", loginRequest); |
| 176 | + Assert.That(loginResponse.IsSuccessStatusCode, Is.True); |
| 177 | + |
| 178 | + var loginResult = await loginResponse.Content.ReadFromJsonAsync<System.Text.Json.JsonElement>(); |
| 179 | + var accessToken = loginResult.GetProperty("accessToken").GetString(); |
| 180 | + Assert.That(accessToken, Is.Not.Null.And.Not.Empty); |
| 181 | + |
| 182 | + // Add token to Authorization header |
| 183 | + HttpClient!.DefaultRequestHeaders.Authorization = |
| 184 | + new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken); |
| 185 | + |
| 186 | + // act - Call protected endpoint with valid bearer token |
| 187 | + var response = await HttpClient!.GetAsync($"{RoutesV2.Prefix}{RoutesV2.Users}"); |
| 188 | + |
| 189 | + // assert - Should succeed with valid authentication |
| 190 | + Assert.That(response.IsSuccessStatusCode, Is.True); |
| 191 | + } |
| 192 | +} |
0 commit comments