private IHttpActionResult CreateDonorForUnauthenticatedUser(CreateDonorDTO dto) { ContactDonor donor; try { donor = _donorService.GetContactDonorForEmail(dto.email_address); } catch (Exception e) { var msg = "Error getting donor for email " + dto.email_address; logger.Error(msg, e); var apiError = new ApiErrorDto(msg, e); throw new HttpResponseException(apiError.HttpResponseMessage); } int existingDonorId = (donor == null) ? 0 : donor.DonorId; try { donor = _donorService.CreateOrUpdateContactDonor(donor, String.Empty, dto.email_address, dto.stripe_token_id, DateTime.Now); } catch (PaymentProcessorException e) { return (e.GetStripeResult()); } catch (Exception e) { var msg = "Error creating donor for email " + dto.email_address; logger.Error(msg, e); var apiError = new ApiErrorDto(msg, e); throw new HttpResponseException(apiError.HttpResponseMessage); } var responseBody = new DonorDTO { Id = donor.DonorId, ProcessorId = donor.ProcessorId, RegisteredUser = false, Email = donor.Email }; // HTTP StatusCode should be 201 (Created) if we created a donor, or 200 (Ok) if returning an existing donor var statusCode = (existingDonorId == donor.DonorId) ? HttpStatusCode.OK : HttpStatusCode.Created; return (ResponseMessage(Request.CreateResponse(statusCode, responseBody))); }
public void ShouldPostToSuccessfullyCreateAuthenticatedDonor() { var createDonorDto = new CreateDonorDTO { stripe_token_id = "tok_test" }; _donorService.Setup(mocked => mocked.GetContactDonorForAuthenticatedUser(It.IsAny<string>())).Returns((ContactDonor)null); _donorService.Setup(mocked => mocked.CreateOrUpdateContactDonor(null, string.Empty, string.Empty, "tok_test", It.IsAny<DateTime>())).Returns(_donor); IHttpActionResult result = _fixture.Post(createDonorDto); Assert.IsNotNull(result); Assert.IsInstanceOf(typeof(OkNegotiatedContentResult<DonorDTO>), result); var okResult = (OkNegotiatedContentResult<DonorDTO>)result; Assert.AreEqual(DonorId, okResult.Content.Id); Assert.AreEqual(ProcessorId, okResult.Content.ProcessorId); }
public void TestCreateDonorForAuthenticatedUserStripeUpdateThrowsStripeException() { var dto = new CreateDonorDTO { email_address = "*****@*****.**", stripe_token_id = "456" }; var contactDonor = new ContactDonor { DonorId = 123, ContactId = 789, Email = "*****@*****.**", ProcessorId = "102030", RegisteredUser = true, }; var stripeException = new PaymentProcessorException(HttpStatusCode.PaymentRequired, "auxMessage", "type", "message", "code", "decline", "param"); _donorService.Setup(mocked => mocked.GetContactDonorForAuthenticatedUser(It.IsAny<string>())).Returns(contactDonor); _donorService.Setup( (mocked => mocked.CreateOrUpdateContactDonor(contactDonor, string.Empty, String.Empty, "456", It.IsAny<DateTime>()))) .Throws(stripeException); var response = _fixture.Post(dto); Assert.AreEqual(typeof(RestHttpActionResult<PaymentProcessorErrorResponse>), response.GetType()); _donorService.VerifyAll(); _paymentService.VerifyAll(); }
public void ShouldThrowExceptionWhenDonorCreationFails() { _fixture.Request.Headers.Authorization = null; var createDonorDto = new CreateDonorDTO { stripe_token_id = "tok_test", email_address = "*****@*****.**" }; var lookupDonor = new ContactDonor { ContactId = 8675309, DonorId = 90210, ProcessorId = "jenny_ive_got_your_number" }; var createException = new Exception("Danger, Will Robinson!"); _donorService.Setup(mocked => mocked.GetContactDonorForEmail(createDonorDto.email_address)).Returns(lookupDonor); _donorService.Setup(mocked => mocked.CreateOrUpdateContactDonor(It.Is<ContactDonor>(d => d == lookupDonor), string.Empty, createDonorDto.email_address, createDonorDto.stripe_token_id, It.IsAny<DateTime>())).Throws(createException); try { _fixture.Post(createDonorDto); Assert.Fail("Expected exception was not thrown"); } catch (Exception e) { Assert.AreEqual(typeof(HttpResponseException), e.GetType()); } _donorService.VerifyAll(); }
public void ShouldThrowExceptionWhenDonorLookupFails() { _fixture.Request.Headers.Authorization = null; var createDonorDto = new CreateDonorDTO { stripe_token_id = "tok_test", email_address = "*****@*****.**" }; var lookupException = new Exception("Danger, Will Robinson!"); _donorService.Setup(mocked => mocked.GetContactDonorForEmail(createDonorDto.email_address)).Throws(lookupException); try { _fixture.Post(createDonorDto); Assert.Fail("Expected exception was not thrown"); } catch (Exception e) { Assert.AreEqual(typeof(HttpResponseException), e.GetType()); } _donorService.VerifyAll(); }
public void ShouldPostToSuccessfullyReturnExistingGuestDonor() { _fixture.Request.Headers.Authorization = null; var createDonorDto = new CreateDonorDTO { stripe_token_id = "tok_test", email_address = "*****@*****.**" }; var lookupDonor = new ContactDonor { ContactId = 8675309, DonorId = 90210, ProcessorId = "jenny_ive_got_your_number" }; var createDonor = new ContactDonor { ContactId = 8675309, DonorId = 90210, ProcessorId = "jenny_ive_got_your_number" }; _donorService.Setup(mocked => mocked.GetContactDonorForEmail(createDonorDto.email_address)).Returns(lookupDonor); _donorService.Setup(mocked => mocked.CreateOrUpdateContactDonor(It.Is<ContactDonor>(d => d == lookupDonor), string.Empty, createDonorDto.email_address, createDonorDto.stripe_token_id, It.IsAny<DateTime>())).Returns(createDonor); IHttpActionResult result = _fixture.Post(createDonorDto); _donorService.VerifyAll(); Assert.IsNotNull(result); Assert.IsInstanceOf(typeof(ResponseMessageResult), result); Assert.NotNull(((ResponseMessageResult)result).Response); Assert.AreEqual(HttpStatusCode.OK, ((ResponseMessageResult)result).Response.StatusCode); var content = ((ResponseMessageResult)result).Response.Content; Assert.NotNull(content); Assert.IsInstanceOf(typeof(ObjectContent<DonorDTO>), content); var responseDto = (DonorDTO)((ObjectContent)content).Value; Assert.AreEqual(90210, responseDto.Id); Assert.AreEqual("jenny_ive_got_your_number", responseDto.ProcessorId); }
private IHttpActionResult CreateDonorForAuthenticatedUser(string authToken, CreateDonorDTO dto) { try { var donor = _donorService.GetContactDonorForAuthenticatedUser(authToken); donor = _donorService.CreateOrUpdateContactDonor(donor, string.Empty, string.Empty, dto.stripe_token_id, DateTime.Now); var response = new DonorDTO { Id = donor.DonorId, ProcessorId = donor.ProcessorId, RegisteredUser = true, Email = donor.Email }; return Ok(response); } catch (PaymentProcessorException e) { return (e.GetStripeResult()); } catch (Exception exception) { var apiError = new ApiErrorDto("Donor Post Failed", exception); throw new HttpResponseException(apiError.HttpResponseMessage); } }