public void GetMyBids_WithCorrectUserToken_ShouldReturnOnlyUsersBidsAnd200Ok()
        {
            // Arrange
            var fakeBids = this.mockContainer.BidRepositoryMock.Object.All();
            var fakeUsers = this.mockContainer.UserRepositoryMock.Object.All();
            var fakeUser = fakeUsers.FirstOrDefault();
            if (fakeUser == null)
            {
                Assert.Fail("Cannot perform test no users available.");
            }

            var mockData = new Mock<IBidSystemData>();
            mockData.Setup(r => r.Bids.All()).Returns(fakeBids.AsQueryable());
            mockData.Setup(r => r.Users.Find(It.IsAny<string>())).Returns(
                (string id) =>
                {
                    return fakeUsers.FirstOrDefault(u => u.Id == id);
                });
            var mockProvider = new Mock<IUserIdProvider>();
            mockProvider.Setup(r => r.GetUserId()).Returns(fakeUser.Id);

            // Act
            var bidsController = new BidsController(mockData.Object, mockProvider.Object);
            this.SetupController(bidsController);
            var httpGetResponse = bidsController.MyBids().ExecuteAsync(CancellationToken.None).Result;
            var result = httpGetResponse.Content.ReadAsAsync<IEnumerable<BidOutputModel>>().Result;

            // Assert
            Assert.AreEqual(HttpStatusCode.OK, httpGetResponse.StatusCode);
            foreach (var bidOutputModel in result)
            {
                Assert.AreEqual(fakeUser.UserName, bidOutputModel.Bidder);
            }
        }
        public void test()
        {
            var bids = new List<Bid>();
            var fakeUser = this.mocks.UserRepositoryMock.Object.All().FirstOrDefault();
            var mockContext = new Mock<IBidSystemData>();
            mockContext.Setup(c => c.Bids)
            .Returns(this.mocks.BidRepositoryMock.Object);
            var mockIdProvider = new Mock<IUserProvider>();
            mockIdProvider.Setup(id => id.GetUserId())
            .Returns(fakeUser.Id);
            this.mocks.BidRepositoryMock
                .Setup(r => r.Add(It.IsAny<Bid>()))
                .Callback((Bid bid) =>
                {
                    bid.BidderId = fakeUser.Id;
                    bid.Bidder = fakeUser;
                    bids.Add(bid);
                });

            var bidsController = new BidsController(mockContext.Object);
            bidsController.UserIdProvider = mockIdProvider.Object;
            this.SetupControllerForTesting(bidsController, "Bids");

            var respone = bidsController.GetBids();
            Assert.AreEqual(2, respone.ToList().Count);

            //this.mocks.BidRepositoryMock
            //    .Setup(r => r.Add(It.IsAny<Bid>()))
            //    .Callback((Bid bid) =>
            //    {
            //        bid.Bidder = fakeUser;
            //        bids.Add(bid);
            //    });

        }
        public void GetMyBids_ShouldReturn200OK_BidsData()
        {
            // Arrange
            this.mockUserIdProvider = new Mock<IUserIdProvider>();
            this.ConfigureMockUserProvider();
            this.bidsController = new BidsController(this.mockContext.Object, this.mockUserIdProvider.Object);
            this.ConfigureController(this.bidsController);

            var fakeUserId = this.mockUserIdProvider.Object.GetUserId();

            // Act
            var response = this.bidsController.UserBids().ExecuteAsync(CancellationToken.None).Result;
            var responseBids = response.Content.ReadAsAsync<List<BidViewModel>>().Result
                .Select(b => b.Id)
                .ToList();

            var expectedBids = this.mockContext.Object.Bids.All()
                .Where(b => b.BidderId.Equals(fakeUserId))
                .OrderByDescending(b => b.BidDate)
                .Select(b => b.Id)
                .ToList();

            // Assert
            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
            CollectionAssert.AreEqual(responseBids, expectedBids);
        }
        public void InitTests()
        {
            this.mocks = new MockContainer();
            this.mocks.PrepareMocks();

            this.mockContext = new Mock<IBidsData>();
            this.ConfigureMockContext();

            this.mockUserIdProvider = new Mock<IUserIdProvider>();

            this.bidsController = new BidsController(
                this.mockContext.Object, this.mockUserIdProvider.Object);
            this.ConfigureController(this.bidsController);
        }
        public void List_User_Bids_Of_Existing_And_Logged_User_Should_List_Bids()
        {
            // Arrange
            var fakeBids = this.mocks.BidRepositoryMock.Object.All();
            var fakeUsers = this.mocks.UserRepositoryMock.Object.All();
            var fakeUser = this.mocks.UserRepositoryMock.Object.All()
                .FirstOrDefault();

            if (fakeUser == null)
            {
                Assert.Fail("Cannot perform test - no users available.");
            }

            var mockContext = new Mock<IBidSystemData>();
            mockContext.Setup(c => c.Bids.All())
                .Returns(fakeBids);
            mockContext.Setup(c => c.Users.All())
                .Returns(fakeUsers);
            var mockUserIdProvider = new Mock<IUserIdProvider>();
            mockUserIdProvider.Setup(uip => uip.GetUserId())
                .Returns(fakeUser.Id);

            var bidsController = new BidsController(mockContext.Object, mockUserIdProvider.Object);
            this.SetupController(bidsController);

            // Act
            var response = bidsController.ListUserBids()
                .ExecuteAsync(CancellationToken.None).Result;
            var results = response.Content.ReadAsAsync<List<BidViewModel>>().Result;
            
            // Assert
            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
            Assert.AreEqual(2, results.Count);
            Assert.AreEqual("gosho", results[0].Bidder);
            Assert.AreEqual("gosho", results[1].Bidder);
            Assert.AreEqual(99, results[0].Id);
            Assert.AreEqual(55, results[1].Id);
        }
 private void ConfigureController(BidsController controller)
 {
     controller.Request = new HttpRequestMessage();
     controller.Configuration = new HttpConfiguration();
 }
        public void GetMyBids_WithIncorrectUserToken_ShouldReturn401NotAuthorized()
        {
            // Arrange
            var fakeBids = this.mockContainer.BidRepositoryMock.Object.All();
            var fakeUsers = this.mockContainer.UserRepositoryMock.Object.All();
            var fakeUser = fakeUsers.FirstOrDefault();
            if (fakeUser == null)
            {
                Assert.Fail("Cannot perform test no users available.");
            }

            var mockData = new Mock<IBidSystemData>();
            mockData.Setup(r => r.Bids.All()).Returns(fakeBids.AsQueryable());
            mockData.Setup(r => r.Users.Find(It.IsAny<string>())).Returns(
                (string id) =>
                {
                    return fakeUsers.FirstOrDefault(u => u.Id == id);
                });
            var mockProvider = new Mock<IUserIdProvider>();

            // Act
            var bidsController = new BidsController(mockData.Object, mockProvider.Object);
            this.SetupController(bidsController);
            var httpGetResponse = bidsController.MyBids().ExecuteAsync(CancellationToken.None).Result;

            // Assert
            Assert.AreEqual(HttpStatusCode.Unauthorized, httpGetResponse.StatusCode);
        }
        public void List_User_Bids_Of_Not_Logged_User_Should_Return_401Unauthorized()
        {
            // Arrange
            var fakeBids = this.mocks.BidRepositoryMock.Object.All();
            var fakeUsers = this.mocks.UserRepositoryMock.Object.All();

            var mockContext = new Mock<IBidSystemData>();
            mockContext.Setup(c => c.Bids.All())
                .Returns(fakeBids);
            mockContext.Setup(c => c.Users.All())
                .Returns(fakeUsers);
            var mockUserIdProvider = new Mock<IUserIdProvider>();

            var bidsController = new BidsController(mockContext.Object, mockUserIdProvider.Object);
            this.SetupController(bidsController);

            // Act
            var response = bidsController.ListUserBids()
                .ExecuteAsync(CancellationToken.None).Result;

            // Assert
            Assert.AreEqual(HttpStatusCode.Unauthorized, response.StatusCode);
        }
 private void SetupController(BidsController bidsController)
 {
     bidsController.Request = new HttpRequestMessage();
     bidsController.Configuration = new HttpConfiguration();
 }