vgquotes/Tests/VideoGameQuotes.Web.Tests/UserProviderTests.cs
tmont 9a1f0347c0 * tests for user provider
* username is no longer required
* added ip address property to user
2011-02-10 21:04:13 +00:00

48 lines
1.7 KiB
C#

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<ISessionStore>();
var user = new User();
session.SetupGet(s => s["user"]).Returns(user).Verifiable();
var userProvider = new SessionBasedUserProvider(new Mock<IUserService>().Object, session.Object, new Mock<HttpContextBase>().Object);
Assert.That(userProvider.CurrentUser, Is.EqualTo(user));
session.VerifyAll();
}
[Test]
public void Should_identify_user_using_ip_address() {
var session = new Mock<ISessionStore>();
session.SetupGet(s => s["user"]).Returns(null);
var httpContext = new Mock<HttpContextBase>();
var httpRequest = new Mock<HttpRequestBase>();
httpRequest.SetupGet(req => req.UserHostAddress).Returns("10.4.60.120");
httpContext.SetupGet(ctx => ctx.Request).Returns(httpRequest.Object);
var userService = new Mock<IUserService>();
userService.Setup(service => service.Save(It.IsAny<User>())).Callback<User>(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 }));
}
}
}