public void DeleteNotAuthorized() { var userId = Guid.NewGuid(); var teamId = Guid.NewGuid(); var visitedPointId = Guid.NewGuid(); SetupUnitOfWorkToPassAuthorizedTest(teamId); var visitedPoint = new VisitedPoint { VisitedPointId = visitedPointId, TeamId = teamId }; var visitedRepositoryMock = new Mock <IGenericRepository <VisitedPoint> >(); visitedRepositoryMock.Setup(r => r.GetByID(It.Is <Guid>(g => g.Equals(visitedPointId)))).Returns(visitedPoint); _UnitOfWorkMock.Setup(u => u.VisitedPointRepository).Returns(visitedRepositoryMock.Object); var userLinkRepositoryMock = new Mock <IGenericRepository <UserLink> >(); _UnitOfWorkMock.Setup(u => u.UserLinkRepository).Returns(userLinkRepositoryMock.Object); var exception = Assert.ThrowsException <BusinessException>(() => _Sut.Delete(userId, visitedPointId)); Assert.AreEqual(BLErrorCodes.UserUnauthorized, exception.ErrorCode); visitedRepositoryMock.Verify(r => r.GetByID(It.Is <Guid>(g => g.Equals(visitedPointId))), Times.Once); visitedRepositoryMock.Verify(r => r.Delete(It.IsAny <VisitedPoint>()), Times.Never); _UnitOfWorkMock.Verify(u => u.Save(), Times.Never); }
public void AddNotAuthorized() { var userId = Guid.NewGuid(); var teamId = Guid.NewGuid(); SetupUnitOfWorkToPassAuthorizedTest(teamId); var visitedRepositoryMock = new Mock <IGenericRepository <VisitedPoint> >(); _UnitOfWorkMock.Setup(u => u.VisitedPointRepository).Returns(visitedRepositoryMock.Object); var userLinkRepositoryMock = new Mock <IGenericRepository <UserLink> >(); _UnitOfWorkMock.Setup(u => u.UserLinkRepository).Returns(userLinkRepositoryMock.Object); var visitedPoint = new VisitedPoint { TeamId = teamId }; var exception = Assert.ThrowsException <BusinessException>(() => _Sut.Add(userId, visitedPoint)); Assert.AreEqual(BLErrorCodes.UserUnauthorized, exception.ErrorCode); visitedRepositoryMock.Verify(r => r.Get(It.IsAny <Expression <Func <VisitedPoint, bool> > >(), It.IsAny <Func <IQueryable <VisitedPoint>, IOrderedQueryable <VisitedPoint> > >(), It.IsAny <string>()), Times.Never); visitedRepositoryMock.Verify(r => r.Insert(It.Is <VisitedPoint>(x => x.Equals(visitedPoint))), Times.Never); _UnitOfWorkMock.Verify(u => u.Save(), Times.Never); }
public void AddDuplicate() { var userId = Guid.NewGuid(); var teamId = Guid.NewGuid(); var pointId = Guid.NewGuid(); var visitedPoint = new VisitedPoint { TeamId = teamId, PointId = pointId }; SetupUnitOfWorkToPassAuthorizedTest(teamId); var visitedRepositoryMock = new Mock <IGenericRepository <VisitedPoint> >(); visitedRepositoryMock.Setup(r => r.Get(It.IsAny <Expression <Func <VisitedPoint, bool> > >(), It.IsAny <Func <IQueryable <VisitedPoint>, IOrderedQueryable <VisitedPoint> > >(), It.IsAny <string>())).Returns(new List <VisitedPoint> { visitedPoint }); _UnitOfWorkMock.Setup(u => u.VisitedPointRepository).Returns(visitedRepositoryMock.Object); var exception = Assert.ThrowsException <BusinessException>(() => _Sut.Add(userId, visitedPoint)); Assert.AreEqual(BLErrorCodes.Duplicate, exception.ErrorCode); Assert.AreEqual($"Visited point with ID '{pointId}' is already known", exception.Message); visitedRepositoryMock.Verify(r => r.Get(It.IsAny <Expression <Func <VisitedPoint, bool> > >(), It.IsAny <Func <IQueryable <VisitedPoint>, IOrderedQueryable <VisitedPoint> > >(), It.IsAny <string>()), Times.Once); visitedRepositoryMock.Verify(r => r.Insert(It.Is <VisitedPoint>(x => x.Equals(visitedPoint))), Times.Never); _UnitOfWorkMock.Verify(u => u.Save(), Times.Never); }
/// <summary> /// This class does something. /// </summary> public async Task <bool> GetVisitedPoint2(int pointId) { string id = RequestContext.Principal.Identity.GetUserId(); VisitedPoint visitedPoint = await db.VisitedPoints.FindAsync(id, pointId); if (visitedPoint == null) { return(false); } return(true); }
public async Task <IHttpActionResult> DeleteVisitedPoint(int pointId) { string id = RequestContext.Principal.Identity.GetUserId();; VisitedPoint visitedPoint = await db.VisitedPoints.FindAsync(id, pointId); if (visitedPoint == null) { return(NotFound()); } db.VisitedPoints.Remove(visitedPoint); await db.SaveChangesAsync(); return(Ok(visitedPoint)); }
public void ModelToViewModelTest() { var model = new VisitedPoint { TeamId = Guid.NewGuid(), PointId = Guid.NewGuid(), Time = DateTime.Now, VisitedPointId = Guid.NewGuid() }; var result = _Sut.Map <VisitedPointViewModel>(model); Assert.AreEqual(model.TeamId, result.TeamId); Assert.AreEqual(model.PointId, result.PointId); Assert.AreEqual(model.Time, result.Time); Assert.AreEqual(model.VisitedPointId, result.VisitedPointId); }
public async Task <IHttpActionResult> PostVisitedPoint(int pointId) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Points3 points3 = await db.Points3.FindAsync(pointId); if (points3 == null) { return(NotFound()); } VisitedPoint visitedPoint = new VisitedPoint(); visitedPoint.Points3 = points3; visitedPoint.UserId = RequestContext.Principal.Identity.GetUserId(); visitedPoint.Points3Id = pointId; db.VisitedPoints.Add(visitedPoint); try { await db.SaveChangesAsync(); } catch (DbUpdateException) { if (VisitedPointExists(visitedPoint.UserId)) { return(Conflict()); } else { throw; } } return(Ok(visitedPoint)); }
public void DeleteWrongAccessLevel() { var userId = Guid.NewGuid(); var teamId = Guid.NewGuid(); var visitedPointId = Guid.NewGuid(); SetupUnitOfWorkToPassAuthorizedTest(teamId); var visitedPoint = new VisitedPoint { VisitedPointId = visitedPointId, TeamId = teamId }; var visitedRepositoryMock = new Mock <IGenericRepository <VisitedPoint> >(); visitedRepositoryMock.Setup(r => r.GetByID(It.Is <Guid>(g => g.Equals(visitedPointId)))).Returns(visitedPoint); _UnitOfWorkMock.Setup(u => u.VisitedPointRepository).Returns(visitedRepositoryMock.Object); var userLinkRepositoryMock = new Mock <IGenericRepository <UserLink> >(); userLinkRepositoryMock.Setup(r => r.Get( It.IsAny <Expression <Func <UserLink, bool> > >(), It.IsAny <Func <IQueryable <UserLink>, IOrderedQueryable <UserLink> > >(), It.IsAny <string>())).Returns(new List <UserLink> { new UserLink { UserId = userId, RaceAccess = RaceAccessLevel.Read } }); _UnitOfWorkMock.Setup(u => u.UserLinkRepository).Returns(userLinkRepositoryMock.Object); var exception = Assert.ThrowsException <BusinessException>(() => _Sut.Delete(userId, visitedPointId)); Assert.AreEqual(BLErrorCodes.UserUnauthorized, exception.ErrorCode); visitedRepositoryMock.Verify(r => r.GetByID(It.Is <Guid>(g => g.Equals(visitedPointId))), Times.Once); visitedRepositoryMock.Verify(r => r.Delete(It.IsAny <VisitedPoint>()), Times.Never); _UnitOfWorkMock.Verify(u => u.Save(), Times.Never); }
public void Add() { var userId = Guid.NewGuid(); var teamId = Guid.NewGuid(); var visitedPoint = new VisitedPoint { TeamId = teamId }; SetupUnitOfWorkToPassAuthorizedTest(teamId); var visitedRepositoryMock = new Mock <IGenericRepository <VisitedPoint> >(); _UnitOfWorkMock.Setup(u => u.VisitedPointRepository).Returns(visitedRepositoryMock.Object); _Sut.Add(userId, visitedPoint); visitedRepositoryMock.Verify(r => r.Get(It.IsAny <Expression <Func <VisitedPoint, bool> > >(), It.IsAny <Func <IQueryable <VisitedPoint>, IOrderedQueryable <VisitedPoint> > >(), It.IsAny <string>()), Times.Once); visitedRepositoryMock.Verify(r => r.Insert(It.Is <VisitedPoint>(x => x.Equals(visitedPoint))), Times.Once); _UnitOfWorkMock.Verify(u => u.Save(), Times.Once); }
public void Delete() { var userId = Guid.NewGuid(); var teamId = Guid.NewGuid(); var visitedPointId = Guid.NewGuid(); var visitedPoint = new VisitedPoint { TeamId = teamId }; SetupUnitOfWorkToPassAuthorizedTest(teamId); var visitedRepositoryMock = new Mock <IGenericRepository <VisitedPoint> >(); visitedRepositoryMock.Setup(r => r.GetByID(It.Is <Guid>(g => g.Equals(visitedPointId)))).Returns(visitedPoint); _UnitOfWorkMock.Setup(u => u.VisitedPointRepository).Returns(visitedRepositoryMock.Object); _Sut.Delete(userId, visitedPointId); visitedRepositoryMock.Verify(r => r.GetByID(It.Is <Guid>(g => g.Equals(visitedPointId))), Times.Once); visitedRepositoryMock.Verify(r => r.Delete(It.Is <VisitedPoint>(g => g.Equals(visitedPoint))), Times.Once); _UnitOfWorkMock.Verify(u => u.Save(), Times.Once); }