public void CreateBid_ValidBids_ShouldCreateBidsCorrectly()
        {
            // Arrange -> clean database, register new user, create an offer
            TestingEngine.CleanDatabase();
            var userSession = TestingEngine.RegisterUser("peter", "pAssW@rd#123456");
            var offerModel = new OfferModel() { Title = "Title", Description = "Description", InitialPrice = 200, ExpirationDateTime = DateTime.Now.AddDays(5) };
            var httpResultOffer = TestingEngine.CreateOfferHttpPost(userSession.Access_Token, offerModel.Title, offerModel.Description, offerModel.InitialPrice, offerModel.ExpirationDateTime);
            Assert.AreEqual(HttpStatusCode.Created, httpResultOffer.StatusCode);
            var offer = httpResultOffer.Content.ReadAsAsync<OfferModel>().Result;

            // Act -> create a few bids
            var bidsToAdds = new BidModel[]
            {
                new BidModel() { BidPrice = 250, Comment = "My initial bid" },
                new BidModel() { BidPrice = 300, Comment = "My second bid" },
                new BidModel() { BidPrice = 400, Comment = "My third bid" },
                new BidModel() { BidPrice = 500 }
            };
            foreach (var bid in bidsToAdds)
            {
                var httpResultBid = TestingEngine.CreateBidHttpPost(userSession.Access_Token, offer.Id, bid.BidPrice, bid.Comment);
                Assert.AreEqual(HttpStatusCode.OK, httpResultBid.StatusCode);
            }

            // Assert -> bids created successfully
            var bidsCount = TestingEngine.GetBidsCountFromDb();
            Assert.AreEqual(4, bidsCount);
        }
        public void CreateBid_InvalidBids_ShouldCreateBidCorrectly()
        {
            // Arrange -> clean database, register new user, create an offer
            TestingEngine.CleanDatabase();
            var userSession = TestingEngine.RegisterUser("peter", "pAssW@rd#123456");
            var offerModel = new OfferModel() { Title = "Title", Description = "Description", InitialPrice = 200, ExpirationDateTime = DateTime.Now.AddDays(5) };
            var httpResultOffer = TestingEngine.CreateOfferHttpPost(userSession.Access_Token, offerModel.Title, offerModel.Description, offerModel.InitialPrice, offerModel.ExpirationDateTime);
            Assert.AreEqual(HttpStatusCode.Created, httpResultOffer.StatusCode);
            var offer = httpResultOffer.Content.ReadAsAsync<OfferModel>().Result;

            // Act -> try to create a few bids
            var bids = new BidModel[]
            {
                new BidModel() { BidPrice = 150, Comment = "Invalid: less than the initioal price" },
                new BidModel() { BidPrice = null, Comment = "Invalid: null price" },
                new BidModel() { BidPrice = 300, Comment = "Valid" },
                new BidModel() { BidPrice = 280, Comment = "Invalid: less than the max price" },
            };
            var httpResultBid0 = TestingEngine.CreateBidHttpPost(userSession.Access_Token, offer.Id, bids[0].BidPrice, bids[0].Comment);
            var httpResultBid1 = TestingEngine.CreateBidHttpPost(userSession.Access_Token, offer.Id, bids[1].BidPrice, bids[1].Comment);
            var httpResultBid2 = TestingEngine.CreateBidHttpPost(userSession.Access_Token, offer.Id, bids[2].BidPrice, bids[2].Comment);
            var httpResultBid3 = TestingEngine.CreateBidHttpPost(userSession.Access_Token, offer.Id, bids[3].BidPrice, bids[3].Comment);

            // Assert -> valid bids are created, invalid not created
            Assert.AreEqual(HttpStatusCode.BadRequest, httpResultBid0.StatusCode);
            Assert.AreEqual(HttpStatusCode.BadRequest, httpResultBid1.StatusCode);
            Assert.AreEqual(HttpStatusCode.OK, httpResultBid2.StatusCode);
            Assert.AreEqual(HttpStatusCode.BadRequest, httpResultBid3.StatusCode);
            var bidsCount = TestingEngine.GetBidsCountFromDb();
            Assert.AreEqual(1, bidsCount);
        }
        public void GetOffer_WithValidId_ShouldReturn200OKAndDataCorectly()
        {
            //Arrange
            TestingEngine.CleanDatabase();
            var userSession = TestingEngine.RegisterUser("peter", "pAssW@rd#123456");
            var offerModel = new OfferModel() { Title = "Title", Description = "Description", InitialPrice = 200, ExpirationDateTime = DateTime.Now.AddDays(5) };
            var httpResultOffer = TestingEngine.CreateOfferHttpPost(userSession.Access_Token, offerModel.Title, offerModel.Description, offerModel.InitialPrice, offerModel.ExpirationDateTime);

          

            // Act 
            var db = new BidSystemDbContext();
            var offerFromDb = db.Offers.FirstOrDefault();
            var bids = new BidModel[]
            {
                new BidModel() { BidPrice = 250, Comment = "Invalid: less than the initioal price" },
            };

            var httpResultBid0 = TestingEngine.CreateBidHttpPost(userSession.Access_Token, offerFromDb.Id, bids[0].BidPrice, bids[0].Comment);

            db.SaveChanges();

            var offerDetailsResponse = TestingEngine.HttpClient.GetAsync("/api/offers/details/" + offerFromDb.Id).Result;
            var offerResponseContent = httpResultOffer.Content.ReadAsAsync<OfferDetailsViewModel>().Result;

            // Assert 
            Assert.AreEqual(HttpStatusCode.OK, offerDetailsResponse.StatusCode);
        }
        public void CreateOffers_ValidOffers_ShouldCreateOffersCorrectly()
        {
            // Arrange -> clean the database and register new user
            TestingEngine.CleanDatabase();
            var userSession = TestingEngine.RegisterUser("peter", "pAssW@rd#123456");

            // Act -> create a few offers
            var offersToAdds = new OfferModel[]
            {
                new OfferModel() { Title = "First Offer (Expired)", Description = "Description", InitialPrice = 200, ExpirationDateTime = DateTime.Now.AddDays(-5)},
                new OfferModel() { Title = "Third Offer (Active 6 months)", InitialPrice = 120, ExpirationDateTime = DateTime.Now.AddMonths(6)},
            };

            foreach (var offer in offersToAdds)
            {
                var httpResult = TestingEngine.CreateOfferHttpPost(userSession.Access_Token, offer.Title, offer.Description, offer.InitialPrice, offer.ExpirationDateTime);
                Assert.AreEqual(HttpStatusCode.Created, httpResult.StatusCode);
            }

            

            // Assert -> offers created correctly
            var offersCount = TestingEngine.GetOffersCountFromDb();
            Assert.AreEqual(2, offersCount);
        }
        public void GetOfferDetails_Of_Existing_Offer_Should_Return_200Ok_And_Offer_Details()
        {
            // Arrange -> clean the database and register new user
            TestingEngine.CleanDatabase();
            var userSession = TestingEngine.RegisterUser("peter", "pAssW@rd#123456");

            // Act -> create a few offers
            var offersToAdds = new OfferModel[]
            {
                new OfferModel() { Title = "First Offer (Expired)", Description = "Description", InitialPrice = 200, ExpirationDateTime = DateTime.Now.AddDays(-5)},
                new OfferModel() { Title = "Another Offer (Expired)", InitialPrice = 15.50m, ExpirationDateTime = DateTime.Now.AddDays(-1)},
                new OfferModel() { Title = "Second Offer (Active 3 months)", Description = "Description", InitialPrice = 500, ExpirationDateTime = DateTime.Now.AddMonths(3)},
                new OfferModel() { Title = "Third Offer (Active 6 months)", InitialPrice = 120, ExpirationDateTime = DateTime.Now.AddMonths(6)},
            };
            foreach (var offer in offersToAdds)
            {
                var httpResult = TestingEngine.CreateOfferHttpPost(userSession.Access_Token, offer.Title, offer.Description, offer.InitialPrice, offer.ExpirationDateTime);
            }

            var firstOfferId = TestingEngine.GetRandomOfferIdFromDb();
            var getOfferDetailsHttpResult = TestingEngine.GetOfferDetailsHttpGet(firstOfferId);
            Assert.AreEqual(HttpStatusCode.OK, getOfferDetailsHttpResult.StatusCode);
            var offerDetails = getOfferDetailsHttpResult.Content.ReadAsAsync<GetOfferByDetailsViewModel>().Result;
            Assert.AreEqual(offersToAdds[0].Title, offerDetails.Title);
            Assert.AreEqual(offersToAdds[0].Description, offerDetails.Description);
            Assert.AreEqual(offersToAdds[0].InitialPrice, offerDetails.InitialPrice);
            //Assert.AreEqual(offersToAdds[0].ExpirationDateTime.ToString(), offerDetails.ExpirationDateTime.ToString(CultureInfo.InvariantCulture));
            Assert.AreEqual(0, offerDetails.BidsCount);
            Assert.IsTrue(offerDetails.IsExpired);
        }
        public void GetOffer_WithInValidId_ShouldReturn404NotFound()
        {
            //Arrange
            TestingEngine.CleanDatabase();
            var userSession = TestingEngine.RegisterUser("peter", "pAssW@rd#123456");
            var offerModel = new OfferModel() { Title = "Title", Description = "Description", InitialPrice = 200, ExpirationDateTime = DateTime.Now.AddDays(5) };
            TestingEngine.CreateOfferHttpPost(userSession.Access_Token, offerModel.Title, offerModel.Description, offerModel.InitialPrice, offerModel.ExpirationDateTime);

            // Act 
            var offerDetailsResponse = TestingEngine.HttpClient.GetAsync("/api/offers/details/" +  -1).Result;

            // Assert 
            Assert.AreEqual(HttpStatusCode.NotFound, offerDetailsResponse.StatusCode);
        }
示例#7
0
        public void CreateOffer_Unauthorized_ShouldReturnUnauthorized()
        {
            // Arrange -> clean the database
            TestingEngine.CleanDatabase();

            // Act -> try to create an offer
            var offer = new OfferModel() { Title = "Title", Description = "Description", InitialPrice = 200, ExpirationDateTime = DateTime.Now.AddDays(5)};
            var httpResult = TestingEngine.CreateOfferHttpPost(null, offer.Title, offer.Description, offer.InitialPrice, offer.ExpirationDateTime);

            // Assert -> offer not created
            Assert.AreEqual(HttpStatusCode.Unauthorized, httpResult.StatusCode);
            var offersCount = TestingEngine.GetOffersCountFromDb();
            Assert.AreEqual(0, offersCount);
        }
示例#8
0
        public void CreateOffers_InvalidOffers_ShouldReturnBadRequest()
        {
            // Arrange -> clean the database
            TestingEngine.CleanDatabase();
            var userSession = TestingEngine.RegisterUser("peter", "pAssW@rd#123456");

            // Act -> try to create a few offers
            var offersToAdds = new OfferModel[]
            {
                new OfferModel() { Description = "Description", InitialPrice = 200, ExpirationDateTime = DateTime.Now.AddDays(-5)},
                new OfferModel() { Title = "Another Offer (Expired)", ExpirationDateTime = DateTime.Now.AddDays(-1)},
                new OfferModel() { Title = "Second Offer (Active 3 months)", Description = "Description", InitialPrice = 500 },
            };
            foreach (var offer in offersToAdds)
            {
                var httpResult = TestingEngine.CreateOfferHttpPost(userSession.Access_Token, offer.Title, offer.Description, offer.InitialPrice, offer.ExpirationDateTime);
                Assert.AreEqual(HttpStatusCode.BadRequest, httpResult.StatusCode);
            }

            // Assert -> offers not created
            var offersCount = TestingEngine.GetOffersCountFromDb();
            Assert.AreEqual(0, offersCount);
        }
        public void CreateBid_Unauthorized_ShouldReturnUnauthorized()
        {
            // Arrange -> clean database, register new user, create an offer
            TestingEngine.CleanDatabase();
            var userSession = TestingEngine.RegisterUser("peter", "pAssW@rd#123456");
            var offerModel = new OfferModel() { Title = "Title", Description = "Description", InitialPrice = 200, ExpirationDateTime = DateTime.Now.AddDays(5) };
            var httpResultOffer = TestingEngine.CreateOfferHttpPost(userSession.Access_Token, offerModel.Title, offerModel.Description, offerModel.InitialPrice, offerModel.ExpirationDateTime);
            Assert.AreEqual(HttpStatusCode.Created, httpResultOffer.StatusCode);
            var offer = httpResultOffer.Content.ReadAsAsync<OfferModel>().Result;

            // Act -> try to create a bid
            var httpResultBid = TestingEngine.CreateBidHttpPost(null, offer.Id, 1000, "Some comment");

            // Assert -> bid not created
            Assert.AreEqual(HttpStatusCode.Unauthorized, httpResultBid.StatusCode);
            var bidsCount = TestingEngine.GetBidsCountFromDb();
            Assert.AreEqual(0, bidsCount);
        }
        private static void CreateFourOfferModelsInDatabase(UserSessionModel userSession)
        {
            var offersToAdds = new OfferModel[]
                               {
                                   new OfferModel()
                                   {
                                       Title = "First Offer (Expired)",
                                       Description = "Description",
                                       InitialPrice = 200,
                                       ExpirationDateTime = DateTime.Now.AddDays(-5)
                                   },
                                   new OfferModel()
                                   {
                                       Title = "Another Offer (Expired)",
                                       InitialPrice = 15.50m,
                                       ExpirationDateTime = DateTime.Now.AddDays(-1)
                                   },
                                   new OfferModel()
                                   {
                                       Title = "Second Offer (Active 3 months)",
                                       Description = "Description",
                                       InitialPrice = 500,
                                       ExpirationDateTime = DateTime.Now.AddMonths(3)
                                   },
                                   new OfferModel()
                                   {
                                       Title = "Third Offer (Active 6 months)",
                                       InitialPrice = 120,
                                       ExpirationDateTime = DateTime.Now.AddMonths(6)
                                   },
                               };

            foreach (var offer in offersToAdds)
            {
                var httpResult = TestingEngine.CreateOfferHttpPost(
                    userSession.Access_Token,
                    offer.Title,
                    offer.Description,
                    offer.InitialPrice,
                    offer.ExpirationDateTime);
                Assert.AreEqual(HttpStatusCode.Created, httpResult.StatusCode);
            }
        }