public async Task PutServer_should_return_bad_request_when_model_is_not_valid() { _serversController.ModelState.AddModelError(string.Empty, string.Empty); var result = await _serversController.PutServer(string.Empty, null); A.CallTo(() => _fakeMediator.Send(A <PutServerInfoCommand> ._, A <CancellationToken> ._)).MustNotHaveHappened(); Assert.IsInstanceOf <InvalidModelStateResult>(result); }
public void PutServer_WithMissing_WillGiveError() { IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 100); using var context = MockDataContext.GetContext(); var controller = new ServersController(context, ConstructChallengeRespondingMock(), logger); MockControllerConnection(controller, endPoint.Address); var result = controller.PutServer(endPoint.ToString(), new GameServer { Name = "Not A" }); // Should have 200 status code, given that the item already exists. Assert.Equal(StatusCodes.Status404NotFound, (result as IStatusCodeActionResult).StatusCode); }
public void PutServer_WithExisting_WillChangeItem() { IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 100); using (var context = MockDataContext.GetContext()) { context.Servers.Add(new GameServer { Name = "Server Not A", Address = endPoint.Address.ToString(), QueryPort = endPoint.Port, GamePort = 100, RoundStatus = "starting", RoundStartTime = DateTime.Now, Game = "SS3D", LastUpdate = DateTime.Now - TimeSpan.FromMinutes(2) }); context.SaveChanges(); } // Use a separate, fresh context for testing. using (var context = MockDataContext.GetContext()) { var controller = new ServersController(context, ConstructChallengeRespondingMock(), logger); MockControllerConnection(controller, endPoint.Address); var result = controller.PutServer(endPoint.ToString(), new GameServer { Name = "Not A", GamePort = 100, RoundStatus = "starting", RoundStartTime = DateTime.Now, Game = "SS3D" }); // Should have 200 status code, given that the item already exists. Assert.Equal(StatusCodes.Status200OK, (result as IStatusCodeActionResult).StatusCode); } using (var postContext = MockDataContext.GetContext()) { var onlyServer = Assert.Single(postContext.Servers); Assert.Equal("Not A", onlyServer.Name); // Check that the LastUpdate has changed to now too Assert.True(onlyServer.LastUpdate > DateTime.Now - TimeSpan.FromSeconds(10)); } }
public void PutServer_WithDifferentAddress_WillGiveError() { IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 100); using (var context = MockDataContext.GetContext()) { context.Servers.Add(new GameServer { Name = "Server Not A", Address = endPoint.Address.ToString(), QueryPort = endPoint.Port, GamePort = 100, RoundStatus = "starting", RoundStartTime = DateTime.Now, Game = "SS3D", LastUpdate = DateTime.Now - TimeSpan.FromMinutes(2) }); context.SaveChanges(); } // Use a separate, fresh context for testing. using (var context = MockDataContext.GetContext()) { var controller = new ServersController(context, ConstructChallengeRespondingMock(), logger); MockControllerConnection(controller, endPoint.Address); var result = controller.PutServer(endPoint.ToString(), new GameServer { Name = "Not A", Address = "128.0.0.1", GamePort = 100, RoundStatus = "starting", RoundStartTime = DateTime.Now, Game = "SS3D" }); // Should have 200 status code, given that the item already exists. Assert.Equal(StatusCodes.Status400BadRequest, (result as IStatusCodeActionResult).StatusCode); } // Just double check for no updates to db using (var postContext = MockDataContext.GetContext()) { var onlyServer = Assert.Single(postContext.Servers); Assert.Equal("Server Not A", onlyServer.Name); } }