Skip to content

Instantly share code, notes, and snippets.

@cubanx
Created March 26, 2012 18:40
Show Gist options
  • Save cubanx/2208591 to your computer and use it in GitHub Desktop.
Save cubanx/2208591 to your computer and use it in GitHub Desktop.
using System;
using System.Web;
using System.Web.Mvc;
using Moq;
using NUnit.Framework;
using TNW.Reporting.Domain.Common;
using TNW.Reporting.Domain.Entities.User;
using TNW.Reporting.Domain.Managers;
using TNW.Reporting.Test.Common;
using TNW.Reporting.Web.Controllers;
using TNW.Reporting.Web.Models.DashboardModels;
namespace TNW.Reporting.Test.Web.Controller
{
[TestFixture]
internal class DashboardControllerTests : BaseTestFixture
{
private static DashboardController GetValidDashboardController() {
return GetValidDashboardController(c => c);
}
private static DashboardController GetValidDashboardController(Func<CompositeUser, CompositeUser> modifyCompositeUser) {
var dashboardController = new DashboardController();
var context = new Mock<ControllerContext>();
var reportingUser = new ReportingUser {LoginToken = Guid.NewGuid()}.Save();
reportingUser.Roles.Add(new ReportingUserRole(Role.TNWAdmin));
var compositeUser = modifyCompositeUser(new CompositeUser(GetFormsIdentity(reportingUser.LoginToken)));
context.SetupGet(r => r.HttpContext.Response.Cookies).Returns(new HttpCookieCollection());
var loginModel = new LoginModel(GetDashboardServiceMock().Object, "[email protected]", "Password1");
loginModel.ValidateUserLogin(loginModel.Username, loginModel.Password);
context.SetupGet(p => p.HttpContext.Session["CM2SessionID"]).Returns(loginModel.CMUserSession.ToString());
context.SetupGet(r => r.HttpContext.Response.Cookies).Returns(new HttpCookieCollection());
dashboardController.ControllerContext = context.Object;
return dashboardController;
}
[Test, Explicit]
public void IndexShouldRedirectIfCurrentUserDoesNotHaveJobCategory() {
using (GetTransaction(Db.Instance, TNW.Common.UserAuthentication.Common.Db.Instance)) {
var dashboardController = GetValidDashboardController();
}
}
[Test, Explicit]
public void IndexShouldReturnViewIfCurrentUserHasJobCategory() {
using (GetTransaction(Db.Instance, TNW.Common.UserAuthentication.Common.Db.Instance)) {
var dashboardController = new DashboardController();
var context = new Mock<ControllerContext>();
var loginModel = new LoginModel(GetDashboardServiceMock().Object, "[email protected]", "Password1");
loginModel.ValidateUserLogin(loginModel.Username, loginModel.Password);
context.SetupGet(p => p.HttpContext.Session["CM2SessionID"]).Returns(loginModel.CMUserSession.ToString());
context.SetupGet(r => r.HttpContext.Response.Cookies).Returns(new HttpCookieCollection());
dashboardController.ControllerContext = context.Object;
Expect(dashboardController.Index(), Returns.Model.TypeOf<IndexModel>());
/*
var dashboardController = GetValidDashboardController(c => { c.ReportingUser = new ReportingUser { DefaultDashboardId = 1 }; return c; });
Expect(dashboardController.Index(), Returns.Model.TypeOf<IndexModel>());
* */
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment