|
1 | | -using System.Web.Mvc; |
2 | | -using System.Web.Routing; |
3 | | -using NSubstitute; |
4 | | -using NUnit.Framework; |
5 | | -using TestStack.FluentMVCTesting.Sample.Controllers; |
6 | | -using TestStack.FluentMVCTesting.Sample.Models; |
7 | | -using TestStack.FluentMVCTesting.Sample.Services; |
8 | | - |
9 | | -namespace TestStack.FluentMVCTesting.Sample.Tests.Controllers |
10 | | -{ |
11 | | - class AccountControllerTests |
12 | | - { |
13 | | - private AccountController _controller; |
14 | | - private IAuthenticationService _authenticationService; |
15 | | - |
16 | | - [SetUp] |
17 | | - public void Setup() |
18 | | - { |
19 | | - _authenticationService = Substitute.For<IAuthenticationService>(); |
20 | | - _controller = new AccountController(_authenticationService) |
21 | | - { |
22 | | - Url = new UrlHelper(Substitute.For<RequestContext>()) |
23 | | - }; |
24 | | - } |
25 | | - |
26 | | - [Test] |
27 | | - public void WhenViewingLoginPage_ThenShowDefaultViewWithReturnUrl() |
28 | | - { |
29 | | - const string returnUrl = "http://www.google.com.au/"; |
30 | | - |
31 | | - _controller.WithCallTo(c => c.Login(returnUrl)) |
32 | | - .ShouldRenderDefaultView(); |
33 | | - |
34 | | - Assert.That(_controller.ViewBag.ReturnUrl, Is.EqualTo(returnUrl)); |
35 | | - } |
36 | | - |
37 | | - [Test] |
38 | | - public void GivenInvalidSubmission_WhenPostingLoginDetails_ThenShowDefaultViewWithInvalidModelAndReturnUrl() |
39 | | - { |
40 | | - var vm = new LoginModel(); |
41 | | - const string returnUrl = "http://www.google.com.au/"; |
42 | | - |
43 | | - _controller.WithModelErrors() |
44 | | - .WithCallTo(c => c.Login(vm, returnUrl)) |
45 | | - .ShouldRenderDefaultView() |
46 | | - .WithModel(vm); |
47 | | - |
48 | | - Assert.That(_controller.ViewBag.ReturnUrl, Is.EqualTo(returnUrl)); |
49 | | - } |
50 | | - |
51 | | - [Test] |
52 | | - public void GivenValidSubmissionButIncorrectDetails_WhenPostingLoginDetails_ThenShowDefaultViewWithInvalidModelAndReturnUrlAndErrorMessage() |
53 | | - { |
54 | | - var vm = new LoginModel(); |
55 | | - const string returnUrl = "http://www.google.com.au/"; |
56 | | - _authenticationService.Login(vm).Returns(false); |
57 | | - |
58 | | - _controller.WithCallTo(c => c.Login(vm, returnUrl)) |
59 | | - .ShouldRenderDefaultView() |
60 | | - .WithModel(vm) |
61 | | - .AndModelError("").ThatEquals("The user name or password provided is incorrect."); |
62 | | - |
63 | | - Assert.That(_controller.ViewBag.ReturnUrl, Is.EqualTo(returnUrl)); |
64 | | - } |
65 | | - |
66 | | - [Test] |
67 | | - public void GivenLocalReturnUrlAndValidSubmission_WhenPostingLoginDetails_ThenLogUserInAndRedirectToReturnUrl() |
68 | | - { |
69 | | - var vm = new LoginModel(); |
70 | | - const string returnUrl = "/localurl"; |
71 | | - _authenticationService.Login(vm).Returns(true); |
72 | | - |
73 | | - _controller.WithCallTo(c => c.Login(vm, returnUrl)) |
74 | | - .ShouldRedirectTo(returnUrl); |
75 | | - } |
76 | | - |
77 | | - [Test] |
78 | | - public void GivenNonLocalReturnUrlAndValidSubmission_WhenPostingLoginDetails_ThenLogUserInAndRedirectToHomepage() |
79 | | - { |
80 | | - var vm = new LoginModel(); |
81 | | - const string returnUrl = "http://www.google.com.au/"; |
82 | | - _authenticationService.Login(vm).Returns(true); |
83 | | - |
84 | | - _controller.WithCallTo(c => c.Login(vm, returnUrl)) |
85 | | - .ShouldRedirectTo<HomeController>(c => c.Index()); |
86 | | - } |
87 | | - |
88 | | - [Test] |
89 | | - public void GivenNoReturnUrlAndValidSubmission_WhenPostingLoginDetails_ThenLogUserInAndRedirectToHomepage([Values(null, "")] string returnUrl) |
90 | | - { |
91 | | - var vm = new LoginModel(); |
92 | | - _authenticationService.Login(vm).Returns(true); |
93 | | - |
94 | | - _controller.WithCallTo(c => c.Login(vm, returnUrl)) |
95 | | - .ShouldRedirectTo<HomeController>(c => c.Index()); |
96 | | - } |
97 | | - } |
98 | | -} |
| 1 | +using System.Web.Mvc; |
| 2 | +using System.Web.Routing; |
| 3 | +using NSubstitute; |
| 4 | +using NUnit.Framework; |
| 5 | +using TestStack.FluentMVCTesting.Sample.Controllers; |
| 6 | +using TestStack.FluentMVCTesting.Sample.Models; |
| 7 | +using TestStack.FluentMVCTesting.Sample.Services; |
| 8 | + |
| 9 | +namespace TestStack.FluentMVCTesting.Sample.Tests.Controllers |
| 10 | +{ |
| 11 | + class AccountControllerTests |
| 12 | + { |
| 13 | + private AccountController _controller; |
| 14 | + private IAuthenticationService _authenticationService; |
| 15 | + |
| 16 | + [SetUp] |
| 17 | + public void Setup() |
| 18 | + { |
| 19 | + _authenticationService = Substitute.For<IAuthenticationService>(); |
| 20 | + _controller = new AccountController(_authenticationService) |
| 21 | + { |
| 22 | + Url = new UrlHelper(Substitute.For<RequestContext>()) |
| 23 | + }; |
| 24 | + } |
| 25 | + |
| 26 | + [Test] |
| 27 | + public void WhenViewingLoginPage_ThenShowDefaultViewWithReturnUrl() |
| 28 | + { |
| 29 | + const string returnUrl = "http://www.google.com.au/"; |
| 30 | + |
| 31 | + _controller.WithCallTo(c => c.Login(returnUrl)) |
| 32 | + .ShouldRenderDefaultView(); |
| 33 | + |
| 34 | + Assert.That(_controller.ViewBag.ReturnUrl, Is.EqualTo(returnUrl)); |
| 35 | + } |
| 36 | + |
| 37 | + [Test] |
| 38 | + public void GivenInvalidSubmission_WhenPostingLoginDetails_ThenShowDefaultViewWithInvalidModelAndReturnUrl() |
| 39 | + { |
| 40 | + var vm = new LoginModel(); |
| 41 | + const string returnUrl = "http://www.google.com.au/"; |
| 42 | + |
| 43 | + _controller.WithModelErrors() |
| 44 | + .WithCallTo(c => c.Login(vm, returnUrl)) |
| 45 | + .ShouldRenderDefaultView() |
| 46 | + .WithModel(vm); |
| 47 | + |
| 48 | + Assert.That(_controller.ViewBag.ReturnUrl, Is.EqualTo(returnUrl)); |
| 49 | + } |
| 50 | + |
| 51 | + [Test] |
| 52 | + public void GivenValidSubmissionButIncorrectDetails_WhenPostingLoginDetails_ThenShowDefaultViewWithInvalidModelAndReturnUrlAndErrorMessage() |
| 53 | + { |
| 54 | + var vm = new LoginModel(); |
| 55 | + const string returnUrl = "http://www.google.com.au/"; |
| 56 | + _authenticationService.Login(vm).Returns(false); |
| 57 | + |
| 58 | + _controller.WithCallTo(c => c.Login(vm, returnUrl)) |
| 59 | + .ShouldRenderDefaultView() |
| 60 | + .WithModel(vm) |
| 61 | + .AndModelError("").ThatEquals("The user name or password provided is incorrect."); |
| 62 | + |
| 63 | + Assert.That(_controller.ViewBag.ReturnUrl, Is.EqualTo(returnUrl)); |
| 64 | + } |
| 65 | + |
| 66 | + [Test] |
| 67 | + public void GivenLocalReturnUrlAndValidSubmission_WhenPostingLoginDetails_ThenLogUserInAndRedirectToReturnUrl() |
| 68 | + { |
| 69 | + var vm = new LoginModel(); |
| 70 | + const string returnUrl = "/localurl"; |
| 71 | + _authenticationService.Login(vm).Returns(true); |
| 72 | + |
| 73 | + _controller.WithCallTo(c => c.Login(vm, returnUrl)) |
| 74 | + .ShouldRedirectTo(returnUrl); |
| 75 | + } |
| 76 | + |
| 77 | + [Test] |
| 78 | + public void GivenNonLocalReturnUrlAndValidSubmission_WhenPostingLoginDetails_ThenLogUserInAndRedirectToHomepage() |
| 79 | + { |
| 80 | + var vm = new LoginModel(); |
| 81 | + const string returnUrl = "http://www.google.com.au/"; |
| 82 | + _authenticationService.Login(vm).Returns(true); |
| 83 | + |
| 84 | + _controller.WithCallTo(c => c.Login(vm, returnUrl)) |
| 85 | + .ShouldRedirectTo<HomeController>(c => c.Index()); |
| 86 | + } |
| 87 | + |
| 88 | + [Test] |
| 89 | + public void GivenNoReturnUrlAndValidSubmission_WhenPostingLoginDetails_ThenLogUserInAndRedirectToHomepage([Values(null, "")] string returnUrl) |
| 90 | + { |
| 91 | + var vm = new LoginModel(); |
| 92 | + _authenticationService.Login(vm).Returns(true); |
| 93 | + |
| 94 | + _controller.WithCallTo(c => c.Login(vm, returnUrl)) |
| 95 | + .ShouldRedirectTo<HomeController>(c => c.Index()); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments