using System.Web; using Moq; using NUnit.Framework; using Portoa.Web.Session; using VideoGameQuotes.Api; using VideoGameQuotes.Api.Persistence; using VideoGameQuotes.Web.Security; namespace VideoGameQuotes.Web.Tests { [TestFixture] public class UserProviderTests { [Test] public void Should_use_user_in_session_store() { var session = new Mock(); var user = new User(); session.SetupGet(s => s["user"]).Returns(user).Verifiable(); var userProvider = new SessionBasedUserProvider(new Mock().Object, session.Object, new Mock().Object); Assert.That(userProvider.CurrentUser, Is.EqualTo(user)); session.VerifyAll(); } [Test] public void Should_identify_user_using_ip_address() { var session = new Mock(); session.SetupGet(s => s["user"]).Returns(null); var httpContext = new Mock(); var httpRequest = new Mock(); httpRequest.SetupGet(req => req.UserHostAddress).Returns("10.4.60.120"); httpContext.SetupGet(ctx => ctx.Request).Returns(httpRequest.Object); var userService = new Mock(); userService.Setup(service => service.Save(It.IsAny())).Callback(user => { Assert.That(user, Is.Not.Null); Assert.That(user.Username, Is.Null); Assert.That(user.IpAddress, Is.EqualTo("10.4.60.120")); Assert.That(user.Group, Is.EqualTo(UserGroup.User)); }).Returns(new User { Id = 1 }); var userProvider = new SessionBasedUserProvider(userService.Object, session.Object, httpContext.Object); Assert.That(userProvider.CurrentUser, Is.EqualTo(new User { Id = 1 })); } } }