private ScheduleSession GetScheduleSession(int registeredUserId, Session session) { return new ScheduleSession { SessionId = session.SessionId, EventDefinitionId = session.EventDefinitionId, Title = session.Title, StartTime = session.StartTime, Duration = session.Duration, TimeZoneOffset = session.TimeZoneOffset, RoomNumber = session.RoomNumber, Speaker = session.Speaker, Description = session.Description, IsFavorite = session.SessionRegisteredUsers.Any(sr => sr.RegisteredUserId == registeredUserId), UserScore = session.SessionRegisteredUsers.Any(sr => sr.RegisteredUserId == registeredUserId) ? session.SessionRegisteredUsers.First(sr => sr.RegisteredUserId == registeredUserId).Score : 0, SessionRegisteredUsers = session.SessionRegisteredUsers != null ? session.SessionRegisteredUsers.Select(ru => new MyEvents.Api.Models.SessionRegisteredUser() { SessionRegisteredUserId = ru.SessionRegisteredUserId, SessionId = ru.SessionId, RegisteredUserId = ru.RegisteredUserId, Score = ru.Score, Rated = ru.Rated, FacebookId = ru.FacebookId }) : null }; }
public void DeleteSession_UnauthorizedException_Test() { var expectedSession = new Session() { SessionId = 1, EventDefinitionId = 1 }; var eventDefinition = new EventDefinition() { OrganizerId = 1 }; IEventDefinitionRepository eventRepository = new StubIEventDefinitionRepository() { GetByIdInt32 = (id) => { Assert.AreEqual(expectedSession.EventDefinitionId, id); return eventDefinition; } }; ISessionRepository sessionRepository = new StubISessionRepository() { GetInt32 = (sessionId) => { Assert.AreEqual(expectedSession.SessionId, sessionId); return expectedSession; } }; using (ShimsContext.Create()) { MyEvents.Api.Authentication.Fakes.ShimMyEventsToken myeventToken = new Authentication.Fakes.ShimMyEventsToken(); myeventToken.RegisteredUserIdGet = () => { return 10000; }; ShimMyEventsToken.GetTokenFromHeader = () => { return myeventToken; }; var target = new SessionsController(sessionRepository, eventRepository); target.Delete(expectedSession.SessionId); } }
public void AddSession_Added_NotFail_Test() { var context = new MyEventsContext(); int eventDefinitionId = context.EventDefinitions.FirstOrDefault().EventDefinitionId; int expected = context.Sessions.Count() + 1; ISessionRepository target = new SessionRepository(); Session session = new Session(); session.EventDefinitionId = eventDefinitionId; session.Title = Guid.NewGuid().ToString(); session.Description = Guid.NewGuid().ToString(); session.Speaker = Guid.NewGuid().ToString(); session.Biography = Guid.NewGuid().ToString(); session.TwitterAccount = Guid.NewGuid().ToString(); session.StartTime = DateTime.Now; session.TimeZoneOffset = 2; session.Duration = 60; target.Add(session); int actual = context.Sessions.Count(); Assert.AreEqual(expected, actual); }
/// <summary> /// <see cref="MyEvents.Data.ISessionRepository"/> /// </summary> /// <param name="session"><see cref="MyEvents.Data.ISessionRepository"/></param> /// <returns><see cref="MyEvents.Data.ISessionRepository"/></returns> public int Add(Session session) { using (var context = new MyEventsContext()) { CheckAndFixTwitterAccount(session); context.Sessions.Add(session); context.SaveChanges(); return session.SessionId; } }
public void DeleteSession_NotFail_Test() { bool called = false; var expectedSession = new Session() { SessionId = 1, EventDefinitionId = 1 }; var eventDefinition = new EventDefinition() { OrganizerId = 1 }; IEventDefinitionRepository eventRepository = new StubIEventDefinitionRepository() { GetByIdInt32 = (id) => { Assert.AreEqual(expectedSession.EventDefinitionId, id); return eventDefinition; } }; ISessionRepository sessionRepository = new StubISessionRepository() { DeleteInt32 = sessionId => { Assert.AreEqual(expectedSession.SessionId, sessionId); called = true; }, GetInt32 = (sessionId) => { Assert.AreEqual(expectedSession.SessionId, sessionId); return expectedSession; } }; using (ShimsContext.Create()) { MyEvents.Api.Authentication.Fakes.ShimMyEventsToken myeventToken = new Authentication.Fakes.ShimMyEventsToken(); myeventToken.RegisteredUserIdGet = () => { return eventDefinition.OrganizerId; }; ShimMyEventsToken.GetTokenFromHeader = () => { return myeventToken; }; var target = new SessionsController(sessionRepository, eventRepository); target.Delete(expectedSession.SessionId); Assert.IsTrue(called); } }
private void SaveSession(Session session) { bool isUpdate = session.SessionId > 0; if (isUpdate) { var sessionToUpdate = _sessionRepository.Get(session.SessionId); MapSession(sessionToUpdate, session); _sessionRepository.Update(sessionToUpdate); } else { _sessionRepository.Add(session); } }
private void MapSession(Session sessionToUpdate, Session session) { sessionToUpdate.Biography = session.Biography; sessionToUpdate.Description = session.Description; sessionToUpdate.RoomNumber = session.RoomNumber; sessionToUpdate.Speaker = session.Speaker; sessionToUpdate.Title = session.Title; sessionToUpdate.TwitterAccount = session.TwitterAccount; }
public ActionResult Save(MyEventsIdentity identity, Session session) { if (!ModelState.IsValid) return PartialView("Form", session); _authorizationService.ValidateSessionAuthorization(identity, session); SaveSession(session); return RedirectToAction("Manage", new { eventDefinitionId = session.EventDefinitionId }); }
public void PutSessionPeriod_UnauthorizedException_Test() { int expectedSessionId = 5; int duration = 10; DateTime startTime = DateTime.Now; var expectedSession = new Session() { SessionId = expectedSessionId, EventDefinitionId = 1 }; var eventDefinition = new EventDefinition() { OrganizerId = 1 }; IEventDefinitionRepository eventRepository = new StubIEventDefinitionRepository() { GetByIdInt32 = (id) => { Assert.AreEqual(expectedSession.EventDefinitionId, id); return eventDefinition; } }; ISessionRepository sessionRepository = new StubISessionRepository() { GetInt32 = (sessionId) => { Assert.AreEqual(expectedSessionId, sessionId); return expectedSession; } }; using (ShimsContext.Create()) { MyEvents.Api.Authentication.Fakes.ShimMyEventsToken myeventToken = new Authentication.Fakes.ShimMyEventsToken(); myeventToken.RegisteredUserIdGet = () => { return 1000; }; ShimMyEventsToken.GetTokenFromHeader = () => { return myeventToken; }; var target = new SessionsController(sessionRepository, eventRepository); target.PutSessionPeriod(expectedSessionId, startTime.ToString(), duration); } }
public void PutSessionPeriod_SessionNotFound_NotFail_Test() { int expectedSessionId = 5; int duration = 10; DateTime startTime = DateTime.Now; bool getCalled = false; bool updateCalled = false; var expectedSession = new Session() { SessionId = expectedSessionId }; IEventDefinitionRepository eventRepository = new StubIEventDefinitionRepository(); ISessionRepository sessionRepository = new StubISessionRepository() { GetInt32 = (sessionId) => { Assert.AreEqual(expectedSessionId, sessionId); getCalled = true; return null; }, UpdateSession = session => { Assert.AreEqual(expectedSessionId, session.SessionId); Assert.AreEqual(startTime, session.StartTime); Assert.AreEqual(duration, session.Duration); updateCalled = true; } }; var target = new SessionsController(sessionRepository, eventRepository); target.PutSessionPeriod(expectedSessionId, startTime.ToString(), duration); Assert.IsTrue(getCalled); Assert.IsFalse(updateCalled); }
/// <summary> /// <see cref="MyEvents.Data.ISessionRepository"/> /// </summary> /// <param name="session"><see cref="MyEvents.Data.ISessionRepository"/></param> public void Update(Session session) { CheckAndFixTwitterAccount(session); using (var context = new MyEventsContext()) { var sessionToUpdate = context.Sessions.FirstOrDefault(q => q.SessionId == session.SessionId); context.Entry<Session>(sessionToUpdate) .CurrentValues .SetValues(session); context.SaveChanges(); } }
public void GetSession_GetResult_NotFail_Test() { int expectedSessionId = 10; bool called = false; var expected = new Session() { SessionId = expectedSessionId }; IEventDefinitionRepository eventRepository = new StubIEventDefinitionRepository(); ISessionRepository sessionRepository = new StubISessionRepository() { GetWithUserInfoInt32Int32 = (userId, eventId) => { Assert.AreEqual(expectedSessionId, eventId); called = true; return expected; } }; using (ShimsContext.Create()) { MyEvents.Api.Authentication.Fakes.ShimMyEventsToken myeventToken = new Authentication.Fakes.ShimMyEventsToken(); myeventToken.RegisteredUserIdGet = () => { return 0; }; ShimMyEventsToken.GetTokenFromHeader = () => { return myeventToken; }; var target = new SessionsController(sessionRepository, eventRepository); Session actual = target.Get(expectedSessionId); Assert.IsTrue(called); Assert.AreEqual(expectedSessionId, actual.SessionId); } }
public int Post(Session session) { if (session == null) throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest)); ValidateEventAuthorization(session.EventDefinitionId); return _sessionRepository.Add(session); }
public void Put(Session session) { if (session == null) throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest)); ValidateEventAuthorization(session.EventDefinitionId); _sessionRepository.Update(session); }
private void CheckAndFixTwitterAccount(Session session) { if (!session.TwitterAccount.StartsWith("@")) session.TwitterAccount = string.Format("@{0}", session.TwitterAccount); }
/// <summary> /// Create action. /// </summary> /// <param name="eventDefinitionId"></param> /// <returns></returns> public ActionResult Create(int eventDefinitionId) { var session = new Session { EventDefinitionId = eventDefinitionId }; return View("Form", session); }
public void PutSessionPeriod_NotFail_Test() { int expectedSessionId = 5; int duration = 10; DateTime startTime = DateTime.Now; bool getCalled = false; bool updateCalled = false; var expectedSession = new Session() { SessionId = expectedSessionId, EventDefinitionId = 1 }; var eventDefinition = new EventDefinition() { OrganizerId = 1 }; IEventDefinitionRepository eventRepository = new StubIEventDefinitionRepository() { GetByIdInt32 = (id) => { Assert.AreEqual(expectedSession.EventDefinitionId, id); return eventDefinition; } }; ISessionRepository sessionRepository = new StubISessionRepository() { GetInt32 = (sessionId) => { Assert.AreEqual(expectedSessionId, sessionId); getCalled = true; return expectedSession; }, UpdateSession = session => { Assert.AreEqual(expectedSessionId, session.SessionId); Assert.AreEqual(duration, session.Duration); updateCalled = true; } }; using (ShimsContext.Create()) { MyEvents.Api.Authentication.Fakes.ShimMyEventsToken myeventToken = new Authentication.Fakes.ShimMyEventsToken(); myeventToken.RegisteredUserIdGet = () => { return eventDefinition.OrganizerId; }; ShimMyEventsToken.GetTokenFromHeader = () => { return myeventToken; }; var target = new SessionsController(sessionRepository, eventRepository); target.PutSessionPeriod(expectedSessionId, startTime.ToString(), duration); Assert.IsTrue(getCalled); Assert.IsTrue(updateCalled); } }
/// <summary> /// Validates the session authorization. /// </summary> /// <param name="identity">The identity.</param> /// <param name="session">The session.</param> /// <exception cref="System.UnauthorizedAccessException"></exception> public void ValidateSessionAuthorization(MyEventsIdentity identity, Session session) { var eventDefinition = _eventDefinitionRepository.GetById(session.EventDefinitionId); if (identity.UserId != eventDefinition.OrganizerId) throw new UnauthorizedAccessException(); }