public async Task<IHttpActionResult> Post(Device device) { Device existing = await deviceRepository.GetDeviceByNameAsync(device.Name); if (existing != null) return Conflict(); device.UserId = User.Identity.GetUserId<int>(); await deviceRepository.CreateAsync(device); return Created($"{Request.RequestUri}{device.Id}", device); }
public async Task PostDeviceWithNonUniqueNameReturnCorrectStatusCode() { using (HttpClient client = await OAuthClient.Create("*****@*****.**", "P4$$word")) { Device device = new Device { Name = "Raspberry Pi" }; await client.PostAsJsonAsync("device", device); HttpResponseMessage response = await client.PostAsJsonAsync("device", device); Assert.True(!response.IsSuccessStatusCode, $"Actual status code: {response.StatusCode}"); response.StatusCode.ShouldEqual(HttpStatusCode.Conflict); } }
public async Task ShouldReturnConflictWhenDeviceWithSameNameAlreadyExist() { string deviceName = "Raspberry Pi"; Device device = new Device { Name = deviceName }; var mock = new Mock<IDeviceRepository>(); mock.Setup(m => m.GetDeviceByNameAsync(deviceName)) .ReturnsAsync(device); IDeviceRepository deviceRepository = mock.Object; IMeasurementUnitRepository measurementUnitRepository = Mock.Of<IMeasurementUnitRepository>(); DeviceController controller = new DeviceController(deviceRepository, measurementUnitRepository); controller.WithCallTo(c => c.Create(device)) .ShouldRenderDefaultView() .WithModel(device); }
public async Task GetAfterPostReturnsResponseWithPostedEntry() { using (HttpClient client = await OAuthClient.Create("*****@*****.**", "P4$$word")) { Device expected = new Device { Name = "Raspberry Pi" }; HttpResponseMessage postResponse = await client.PostAsJsonAsync("device", expected); HttpResponseMessage getResponse = await client.GetAsync("device"); IEnumerable<Device> devices = await getResponse.Content.ReadAsAsync<IEnumerable<Device>>(); expected.Id = 1; Assert.True(postResponse.IsSuccessStatusCode, $"Actual status code: {postResponse.StatusCode}"); postResponse.StatusCode.ShouldEqual(HttpStatusCode.Created); devices.Count().ShouldEqual(1); Assert.Contains(expected, devices, new DeviceComparer()); } }
public async Task<ActionResult> Create(Device device) { if (device == null || !ModelState.IsValid) { ModelState.AddModelError("", "Failed to create device."); return View(device); } Device existingDevice = await deviceRepository.GetDeviceByNameAsync(device.Name); if (existingDevice != null) { ModelState.AddModelError(nameof(device.Name), "A device with the entered name already exists. Please choose another name"); return View(device); } device.UserId = User.Identity.GetUserId<int>(); await deviceRepository.CreateAsync(device); return RedirectToAction("Details", new { id = device.Id }); }
public async Task ShouldRedirectWhenDeviceIsCreated() { Device device = new Device { Name = "Raspberry Pi" }; var mock = new Mock<IDeviceRepository>(); mock.Setup(m => m.GetDeviceByNameAsync(device.Name)) .ReturnsAsync(null); var context = new Mock<HttpContextBase>(); var identity = new GenericIdentity("*****@*****.**"); identity.AddClaim(new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier", "1")); var principal = new GenericPrincipal(identity, new[] { "user" }); context.Setup(s => s.User).Returns(principal); IDeviceRepository deviceRepository = mock.Object; IMeasurementUnitRepository measurementUnitRepository = Mock.Of<IMeasurementUnitRepository>(); DeviceController controller = new DeviceController(deviceRepository, measurementUnitRepository); controller.ControllerContext = new ControllerContext(context.Object, new RouteData(), controller); controller.WithCallTo(c => c.Create(device)) .ShouldRedirectTo(c => c.Details(1)); }
public async Task CreateAsync(Device device) { Context.Devices.Add(device); await Context.SaveChangesAsync(); }
public async Task ShouldStoreGivenDevices() { Device device = new Device { Name = "Raspberry Pi", UserId = 1 }; await Repository.CreateAsync(device); device.Id.ShouldBeGreaterThan(0); }
public async Task ShouldReturnStoredDeviceByName() { Device device = new Device { Name = "Raspberry Pi", UserId = 1 }; await Repository.CreateAsync(device); Device expected = await Repository.GetDeviceByNameAsync(device.Name); expected.ShouldNotBeNull(); expected.Name.ShouldEqual(device.Name); }