|
| 1 | +using exercise.wwwapi.DTOs.Posts; |
| 2 | +using Microsoft.AspNetCore.Http; |
| 3 | +using Microsoft.AspNetCore.Mvc.Testing; |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Linq; |
| 7 | +using System.Net; |
| 8 | +using System.Text; |
| 9 | +using System.Text.Json; |
| 10 | +using System.Text.Json.Nodes; |
| 11 | +using System.Threading.Tasks; |
| 12 | + |
| 13 | +namespace exercise.tests.IntegrationTests |
| 14 | +{ |
| 15 | + [TestFixture] |
| 16 | + public class PostTests |
| 17 | + { |
| 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 | + |
| 36 | + [Test] |
| 37 | + public async Task SuccessfulCreatePostStatus() |
| 38 | + { |
| 39 | + int userid = 1; |
| 40 | + string content = "Some content"; |
| 41 | + CreatePostDTO body = new CreatePostDTO |
| 42 | + { |
| 43 | + Userid = userid, |
| 44 | + Content = content |
| 45 | + }; |
| 46 | + var json = JsonSerializer.Serialize(body); |
| 47 | + var requestBody = new StringContent(json, Encoding.UTF8, "application/json"); |
| 48 | + |
| 49 | + // Act |
| 50 | + var response = await _client.PostAsync("/posts", requestBody); |
| 51 | + |
| 52 | + var contentString = await response.Content.ReadAsStringAsync(); |
| 53 | + |
| 54 | + JsonNode? message = null; |
| 55 | + if (!string.IsNullOrWhiteSpace(contentString)) |
| 56 | + { |
| 57 | + message = JsonNode.Parse(contentString); |
| 58 | + } |
| 59 | + |
| 60 | + Console.WriteLine("Message: " + message); |
| 61 | + // Assert |
| 62 | + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.Created)); |
| 63 | + } |
| 64 | + |
| 65 | + [Test] |
| 66 | + public async Task SuccessfulCreatePostMessage() |
| 67 | + { |
| 68 | + int userid = 1; |
| 69 | + string content = "Some content"; |
| 70 | + CreatePostDTO body = new CreatePostDTO |
| 71 | + { |
| 72 | + Userid = userid, |
| 73 | + Content = content |
| 74 | + }; |
| 75 | + var json = JsonSerializer.Serialize(body); |
| 76 | + var requestBody = new StringContent(json, Encoding.UTF8, "application/json"); |
| 77 | + |
| 78 | + // Act |
| 79 | + var response = await _client.PostAsync("/posts", requestBody); |
| 80 | + |
| 81 | + var contentString = await response.Content.ReadAsStringAsync(); |
| 82 | + |
| 83 | + JsonNode? message = null; |
| 84 | + if (!string.IsNullOrWhiteSpace(contentString)) |
| 85 | + { |
| 86 | + message = JsonNode.Parse(contentString); |
| 87 | + } |
| 88 | + |
| 89 | + Console.WriteLine("Message: " + message); |
| 90 | + // Assert |
| 91 | + |
| 92 | + // Assert JSON structure |
| 93 | + Assert.That(message, Is.Not.Null); |
| 94 | + Assert.That(message?["message"]?.GetValue<string>(), Is.EqualTo("success")); |
| 95 | + |
| 96 | + var data = message["data"]; |
| 97 | + Assert.That(data, Is.Not.Null); |
| 98 | + Assert.That(data?["id"]?.GetValue<int>(), Is.GreaterThan(0)); |
| 99 | + Assert.That(data["content"]?.GetValue<string>(), Is.EqualTo(content)); |
| 100 | + Assert.That(data["numLikes"]?.GetValue<int>(), Is.EqualTo(0)); |
| 101 | + Assert.That(data["comments"]?.AsArray().Count, Is.EqualTo(0)); |
| 102 | + |
| 103 | + // Check User object inside data |
| 104 | + var user = data["user"]; |
| 105 | + Assert.That(user, Is.Not.Null); |
| 106 | + Assert.That(user!["id"]?.GetValue<int>(), Is.EqualTo(userid)); |
| 107 | + Assert.That(user["firstName"]?.GetValue<string>(), Is.Not.Null); |
| 108 | + Assert.That(user["lastName"]?.GetValue<string>(), Is.Not.Null); |
| 109 | + Assert.That(user["photo"]?.GetValue<string>(), Is.Not.Null); |
| 110 | + |
| 111 | + Assert.That(message["timestamp"], Is.Not.Null); |
| 112 | + Assert.That(data["createdAt"], Is.Not.Null); |
| 113 | + } |
| 114 | + |
| 115 | + [TestCase(9999999, "somecontent", HttpStatusCode.NotFound)] |
| 116 | + [TestCase(5, "", HttpStatusCode.BadRequest)] |
| 117 | + [TestCase(5, " ", HttpStatusCode.BadRequest)] |
| 118 | + [TestCase(5, " ", HttpStatusCode.BadRequest)] |
| 119 | + public async Task FailedlCreatePostStatus(int userid, string content, HttpStatusCode expected) |
| 120 | + { |
| 121 | + CreatePostDTO body = new CreatePostDTO |
| 122 | + { |
| 123 | + Userid = userid, |
| 124 | + Content = content |
| 125 | + }; |
| 126 | + var json = JsonSerializer.Serialize(body); |
| 127 | + var requestBody = new StringContent(json, Encoding.UTF8, "application/json"); |
| 128 | + |
| 129 | + // Act |
| 130 | + var response = await _client.PostAsync("/posts", requestBody); |
| 131 | + |
| 132 | + var contentString = await response.Content.ReadAsStringAsync(); |
| 133 | + |
| 134 | + JsonNode? message = null; |
| 135 | + if (!string.IsNullOrWhiteSpace(contentString)) |
| 136 | + { |
| 137 | + message = JsonNode.Parse(contentString); |
| 138 | + } |
| 139 | + |
| 140 | + Console.WriteLine("Message: " + message); |
| 141 | + // Assert |
| 142 | + Assert.That(response.StatusCode, Is.EqualTo(expected)); |
| 143 | + } |
| 144 | + |
| 145 | + [TestCase(9999999, "somecontent", "Invalid userID")] |
| 146 | + [TestCase(5, "", "Content cannot be empty")] |
| 147 | + [TestCase(5, " ", "Content cannot be empty")] |
| 148 | + [TestCase(5, " ", "Content cannot be empty")] |
| 149 | + public async Task FailedCreatePostMessage(int userid, string content, string expectedmessage) |
| 150 | + { |
| 151 | + CreatePostDTO body = new CreatePostDTO |
| 152 | + { |
| 153 | + Userid = userid, |
| 154 | + Content = content |
| 155 | + }; |
| 156 | + var json = JsonSerializer.Serialize(body); |
| 157 | + var requestBody = new StringContent(json, Encoding.UTF8, "application/json"); |
| 158 | + |
| 159 | + // Act |
| 160 | + var response = await _client.PostAsync("/posts", requestBody); |
| 161 | + |
| 162 | + var contentString = await response.Content.ReadAsStringAsync(); |
| 163 | + |
| 164 | + JsonNode? message = null; |
| 165 | + if (!string.IsNullOrWhiteSpace(contentString)) |
| 166 | + { |
| 167 | + message = JsonNode.Parse(contentString); |
| 168 | + } |
| 169 | + |
| 170 | + Console.WriteLine("Message: " + message); |
| 171 | + |
| 172 | + // Assert JSON structure |
| 173 | + Assert.That(message, Is.Not.Null); |
| 174 | + Assert.That(message?["message"]?.GetValue<string>(), Is.EqualTo(expectedmessage)); |
| 175 | + Assert.That(message["data"], Is.Null); |
| 176 | + Assert.That(message["timestamp"], Is.Not.Null); |
| 177 | + } |
| 178 | + [Test] |
| 179 | + public async Task DeletePostById_SuccessAndNotFound() |
| 180 | + { |
| 181 | + // Arrange, create a post first |
| 182 | + var newPost = new CreatePostDTO |
| 183 | + { |
| 184 | + Userid = 1, |
| 185 | + Content = "Temp post to delete" |
| 186 | + }; |
| 187 | + var json = JsonSerializer.Serialize(newPost); |
| 188 | + var requestBody = new StringContent(json, Encoding.UTF8, "application/json"); |
| 189 | + |
| 190 | + var createResponse = await _client.PostAsync("/posts", requestBody); |
| 191 | + var createContent = await createResponse.Content.ReadAsStringAsync(); |
| 192 | + var createMessage = JsonNode.Parse(createContent); |
| 193 | + |
| 194 | + int createdPostId = createMessage!["data"]!["id"]!.GetValue<int>(); |
| 195 | + |
| 196 | + // Act, delete the post |
| 197 | + var deleteResponse = await _client.DeleteAsync($"/posts/{createdPostId}"); |
| 198 | + var deleteContent = await deleteResponse.Content.ReadAsStringAsync(); |
| 199 | + |
| 200 | + // Assert delete success |
| 201 | + Assert.That(deleteResponse.StatusCode, Is.EqualTo(HttpStatusCode.OK)); |
| 202 | + |
| 203 | + // Act again, try deleting same post (should now be gone) |
| 204 | + var deleteAgainResponse = await _client.DeleteAsync($"/posts/{createdPostId}"); |
| 205 | + var deleteAgainContent = await deleteAgainResponse.Content.ReadAsStringAsync(); |
| 206 | + var deleteAgainMessage = JsonNode.Parse(deleteAgainContent); |
| 207 | + |
| 208 | + // Assert not found |
| 209 | + Assert.That(deleteAgainResponse.StatusCode, Is.EqualTo(HttpStatusCode.NotFound)); |
| 210 | + Assert.That(deleteAgainMessage?["message"]?.GetValue<string>(), Is.EqualTo("Post not found")); |
| 211 | + } |
| 212 | + |
| 213 | + |
| 214 | + |
| 215 | + |
| 216 | + [TestCase(5, "Updated content", HttpStatusCode.OK)] |
| 217 | + [TestCase(9999999, "Updated content", HttpStatusCode.NotFound)] |
| 218 | + [TestCase(5, "", HttpStatusCode.BadRequest)] |
| 219 | + public async Task UpdatePostById(int postId, string newContent, HttpStatusCode expected) |
| 220 | + { |
| 221 | + // Arrange |
| 222 | + UpdatePostDTO body = new UpdatePostDTO { Content = newContent }; |
| 223 | + var json = JsonSerializer.Serialize(body); |
| 224 | + var requestBody = new StringContent(json, Encoding.UTF8, "application/json"); |
| 225 | + |
| 226 | + // Act |
| 227 | + var response = await _client.PatchAsync($"/posts/{postId}", requestBody); |
| 228 | + var contentString = await response.Content.ReadAsStringAsync(); |
| 229 | + |
| 230 | + JsonNode? message = null; |
| 231 | + if (!string.IsNullOrWhiteSpace(contentString)) |
| 232 | + { |
| 233 | + message = JsonNode.Parse(contentString); |
| 234 | + } |
| 235 | + |
| 236 | + Console.WriteLine("Message: " + message); |
| 237 | + |
| 238 | + // Assert status |
| 239 | + Assert.That(response.StatusCode, Is.EqualTo(expected)); |
| 240 | + |
| 241 | + if (expected == HttpStatusCode.OK) |
| 242 | + { |
| 243 | + var data = message?["data"]; |
| 244 | + Assert.That(data, Is.Not.Null); |
| 245 | + Assert.That(data?["id"]?.GetValue<int>(), Is.EqualTo(postId)); |
| 246 | + Assert.That(data?["content"]?.GetValue<string>(), Is.EqualTo(newContent)); |
| 247 | + Assert.That(data?["updatedAt"], Is.Not.Null); |
| 248 | + } |
| 249 | + else if (expected == HttpStatusCode.NotFound) |
| 250 | + { |
| 251 | + Assert.That(message?["message"]?.GetValue<string>(), Is.EqualTo("Post not found")); |
| 252 | + Assert.That(message?["data"], Is.Null); |
| 253 | + } |
| 254 | + else if (expected == HttpStatusCode.BadRequest) |
| 255 | + { |
| 256 | + Assert.That(message?["message"]?.GetValue<string>(), Is.EqualTo("Content cannot be empty")); |
| 257 | + Assert.That(message?["data"], Is.Null); |
| 258 | + } |
| 259 | + } |
| 260 | + |
| 261 | + |
| 262 | + } |
| 263 | +} |
0 commit comments