public async Task WhenVoucherIsAlreadyClaimed_ShouldGetForbidden() { var voucherTypeDto = new VoucherTypeDto(); voucherTypeDto.VoucherType = VoucherTypeEnum.FreeShipping; Guid voucherId = Guid.Empty; using (var jsonContent = JsonStringContent(JsonConvert.SerializeObject(voucherTypeDto))) { HttpResponseMessage response = await _client.PostAsync("vouchers", jsonContent); response.StatusCode.Should().Be(HttpStatusCode.Created, "test setup: the voucher should have been created"); var createdVoucher = JsonConvert.DeserializeObject <ClaimedVoucherDto>(await response.Content.ReadAsStringAsync()); voucherId = createdVoucher.Id; } var claimAttempt1 = await _client.PostAsync($"vouchers/{voucherId}/claim", new StringContent("")); claimAttempt1.StatusCode.Should().Be(HttpStatusCode.OK, "test setup: the initial claim attempt of the voucher should succeed"); var claimAttempt2 = await _client.PostAsync($"vouchers/{voucherId}/claim", new StringContent("")); claimAttempt2.StatusCode.Should().Be(HttpStatusCode.Forbidden, "voucher can not be claimed for the second time"); }
public async Task WhenSupplyingUnknownVoucherType_ShouldNotGenerateVoucher() { var voucherTypeDto = new VoucherTypeDto(); voucherTypeDto.VoucherType = VoucherTypeEnum.ThisShouldLeadToFailures; using (var jsonContent = JsonStringContent(JsonConvert.SerializeObject(voucherTypeDto))) { HttpResponseMessage response = await _client.PostAsync("vouchers", jsonContent); response.StatusCode.Should().Be(HttpStatusCode.BadRequest, "we've supplied incorrect information on purpose"); } }
public async Task WhenPerformingValidRequest_ShouldReturn201Created() { var voucherTypeDto = new VoucherTypeDto(); voucherTypeDto.VoucherType = VoucherTypeEnum.FreeShipping; using (var jsonContent = JsonStringContent(JsonConvert.SerializeObject(voucherTypeDto))) { HttpResponseMessage response = await _client.PostAsync("vouchers", jsonContent); response.StatusCode.Should().Be(HttpStatusCode.Created, "the request is properly formatted and contains valid contents, " + "which should lead to the creation of a voucher"); } }
public ActionResult <NewVoucher> Post([FromBody] VoucherTypeDto voucherTypeDto) { VoucherType voucherType = (VoucherType)((int)voucherTypeDto.VoucherType); try { var newVoucherDto = NewVoucherDto.FromDomain(_createVoucherUseCase.Create(voucherType)); _logger.Information("Voucher {voucherId} successfully created", newVoucherDto.Id); return(StatusCode((int)HttpStatusCode.Created, newVoucherDto)); } catch (Exception e) { _logger.Error(e, "Generic exception occurred while creating voucher {voucherType}", voucherTypeDto.VoucherType); return(StatusCode((int)HttpStatusCode.InternalServerError, "Could not create a voucher code.")); } }