|
| 1 | +// |
| 2 | +// TestAuthenticationService.cs |
| 3 | +// |
| 4 | +// Author: |
| 5 | +// Craig Fowler <craig@craigfowler.me.uk> |
| 6 | +// |
| 7 | +// Copyright (c) 2016 Craig Fowler |
| 8 | +// |
| 9 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | +// of this software and associated documentation files (the "Software"), to deal |
| 11 | +// in the Software without restriction, including without limitation the rights |
| 12 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | +// copies of the Software, and to permit persons to whom the Software is |
| 14 | +// furnished to do so, subject to the following conditions: |
| 15 | +// |
| 16 | +// The above copyright notice and this permission notice shall be included in |
| 17 | +// all copies or substantial portions of the Software. |
| 18 | +// |
| 19 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | +// THE SOFTWARE. |
| 26 | +using System; |
| 27 | +using CSF.Security; |
| 28 | +using NUnit.Framework; |
| 29 | +using Moq; |
| 30 | + |
| 31 | +namespace Test.CSF.Security |
| 32 | +{ |
| 33 | + [TestFixture] |
| 34 | + public class TestAuthenticationService |
| 35 | + { |
| 36 | + #region fields |
| 37 | + |
| 38 | + private Mock<ICredentialsRepository<StubEnteredCredentials,StubStoredCredentials>> _repository; |
| 39 | + private Mock<ICredentialVerifier<StubEnteredCredentials,StubStoredCredentials>> _verifier; |
| 40 | + |
| 41 | + private IAuthenticationService<StubEnteredCredentials> _sut; |
| 42 | + |
| 43 | + #endregion |
| 44 | + |
| 45 | + #region setup |
| 46 | + |
| 47 | + [SetUp] |
| 48 | + public void Setup() |
| 49 | + { |
| 50 | + _repository = new Mock<ICredentialsRepository<StubEnteredCredentials, StubStoredCredentials>>(); |
| 51 | + _verifier = new Mock<ICredentialVerifier<StubEnteredCredentials, StubStoredCredentials>>(); |
| 52 | + |
| 53 | + _sut = new AuthenticationService<StubEnteredCredentials,StubStoredCredentials>(_repository.Object, |
| 54 | + _verifier.Object); |
| 55 | + } |
| 56 | + |
| 57 | + #endregion |
| 58 | + |
| 59 | + #region tests |
| 60 | + |
| 61 | + [Test] |
| 62 | + public void Authenticate_returns_not_found_result_when_credentials_not_found() |
| 63 | + { |
| 64 | + // Arrange |
| 65 | + var entered = Mock.Of<StubEnteredCredentials>(); |
| 66 | + |
| 67 | + _repository.Setup(x => x.GetStoredCredentials(entered)).Returns((StubStoredCredentials) null); |
| 68 | + |
| 69 | + // Act |
| 70 | + var result = _sut.Authenticate(entered); |
| 71 | + |
| 72 | + // Assert |
| 73 | + Assert.IsFalse(result.CredentialsFound); |
| 74 | + } |
| 75 | + |
| 76 | + [Test] |
| 77 | + public void Authenticate_returns_failure_result_when_credentials_not_found() |
| 78 | + { |
| 79 | + // Arrange |
| 80 | + var entered = Mock.Of<StubEnteredCredentials>(); |
| 81 | + |
| 82 | + _repository.Setup(x => x.GetStoredCredentials(entered)).Returns((StubStoredCredentials) null); |
| 83 | + |
| 84 | + // Act |
| 85 | + var result = _sut.Authenticate(entered); |
| 86 | + |
| 87 | + // Assert |
| 88 | + Assert.IsFalse(result.CredentialsVerified); |
| 89 | + } |
| 90 | + |
| 91 | + [Test] |
| 92 | + public void Authenticate_returns_found_result_when_credentials_are_found() |
| 93 | + { |
| 94 | + // Arrange |
| 95 | + var entered = Mock.Of<StubEnteredCredentials>(); |
| 96 | + var stored = Mock.Of<StubStoredCredentials>(); |
| 97 | + |
| 98 | + _repository.Setup(x => x.GetStoredCredentials(entered)).Returns(stored); |
| 99 | + |
| 100 | + // Act |
| 101 | + var result = _sut.Authenticate(entered); |
| 102 | + |
| 103 | + // Assert |
| 104 | + Assert.IsTrue(result.CredentialsFound); |
| 105 | + } |
| 106 | + |
| 107 | + [Test] |
| 108 | + public void Authenticate_returns_failure_result_when_authentication_fails() |
| 109 | + { |
| 110 | + // Arrange |
| 111 | + var entered = Mock.Of<StubEnteredCredentials>(); |
| 112 | + var stored = Mock.Of<StubStoredCredentials>(); |
| 113 | + |
| 114 | + _repository.Setup(x => x.GetStoredCredentials(entered)).Returns(stored); |
| 115 | + _verifier.Setup(x => x.Verify(entered, stored)).Returns(false); |
| 116 | + |
| 117 | + // Act |
| 118 | + var result = _sut.Authenticate(entered); |
| 119 | + |
| 120 | + // Assert |
| 121 | + Assert.IsFalse(result.CredentialsVerified); |
| 122 | + } |
| 123 | + |
| 124 | + [Test] |
| 125 | + public void Authenticate_returns_success_result_when_authentication_passes() |
| 126 | + { |
| 127 | + // Arrange |
| 128 | + var entered = Mock.Of<StubEnteredCredentials>(); |
| 129 | + var stored = Mock.Of<StubStoredCredentials>(); |
| 130 | + |
| 131 | + _repository.Setup(x => x.GetStoredCredentials(entered)).Returns(stored); |
| 132 | + _verifier.Setup(x => x.Verify(entered, stored)).Returns(true); |
| 133 | + |
| 134 | + // Act |
| 135 | + var result = _sut.Authenticate(entered); |
| 136 | + |
| 137 | + // Assert |
| 138 | + Assert.IsTrue(result.CredentialsVerified); |
| 139 | + } |
| 140 | + |
| 141 | + #endregion |
| 142 | + |
| 143 | + #region contained types |
| 144 | + |
| 145 | + public class StubStoredCredentials : Base64KeyAndSalt {} |
| 146 | + |
| 147 | + public class StubEnteredCredentials : ICredentialsWithPassword |
| 148 | + { |
| 149 | + public virtual byte[] GetPasswordAsByteArray() |
| 150 | + { |
| 151 | + throw new NotImplementedException(); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + #endregion |
| 156 | + } |
| 157 | +} |
| 158 | + |
0 commit comments