Example #1
0
        public void ToValue_WhenByteArrayCanBeConvertedToUTF8_AssertUnprotectWasCalledOnDataProtector()
        {
            IContentHelper sut = CreateSut();

            byte[] byteArray = BuildStringValueAsByteArray(Guid.NewGuid().ToString("D"));
            sut.ToValue(byteArray);

            _dataProtectorMock.Verify(m => m.Unprotect(It.Is <byte[]>(value => value != null && string.CompareOrdinal(Encoding.UTF8.GetString(value), Encoding.UTF8.GetString(byteArray)) == 0)), Times.Once);
        }
Example #2
0
        public void ToValue_WhenBase64StringIsNotBase64String_ReturnsNull()
        {
            IContentHelper sut = CreateSut();

            string base64String = Guid.NewGuid().ToString("D");
            string result       = sut.ToValue(base64String);

            Assert.IsNull(result);
        }
Example #3
0
        public void ToValue_WhenByteArrayCanBeConvertedToUTF8_AssertCreateProtectorWasCalledOnDataProtectionProviderWithValueProtection()
        {
            IContentHelper sut = CreateSut();

            byte[] byteArray = BuildStringValueAsByteArray(Guid.NewGuid().ToString("D"));
            sut.ToValue(byteArray);

            _dataProtectionProviderMock.Verify(m => m.CreateProtector(It.Is <string>(value => string.CompareOrdinal(value, "ValueProtection") == 0)), Times.Exactly(2));
        }
Example #4
0
        public void ToValue_WhenByteArrayCanBeConvertedToUTF8AndCryptographicExceptionWasThrown_ReturnsNull()
        {
            CryptographicException exception = new CryptographicException();
            IContentHelper         sut       = CreateSut(exception);

            byte[] byteArray = BuildStringValueAsByteArray();
            string result    = sut.ToValue(byteArray);

            Assert.IsNull(result);
        }
Example #5
0
        public void ToValue_WhenBase64StringCanBeConvertedToUTF8AndCryptographicExceptionWasThrown_ReturnsNull()
        {
            CryptographicException exception = new CryptographicException();
            IContentHelper         sut       = CreateSut(exception);

            string base64String = BuildStringValueAsBase64String();
            string result       = sut.ToValue(base64String);

            Assert.IsNull(result);
        }
Example #6
0
        public void ToValue_WhenBase64StringCanBeConvertedToUTF8_AssertUnprotectWasCalledOnDataProtector()
        {
            IContentHelper sut = CreateSut();

            string base64String = BuildStringValueAsBase64String(Guid.NewGuid().ToString("D"));

            sut.ToValue(base64String);

            _dataProtectorMock.Verify(m => m.Unprotect(It.Is <byte[]>(value => value != null && string.CompareOrdinal(Convert.ToBase64String(value), base64String) == 0)), Times.Once);
        }
Example #7
0
        public void ToValue_WhenBase64StringIsNotBase64String_AssertUnprotectWasNotCalledOnDataProtector()
        {
            IContentHelper sut = CreateSut();

            string base64String = Guid.NewGuid().ToString("D");

            sut.ToValue(base64String);

            _dataProtectorMock.Verify(m => m.Unprotect(It.IsAny <byte[]>()), Times.Never);
        }
Example #8
0
        public void ToValue_WhenBase64StringIsNotBase64String_AssertCreateProtectorNotWasCalledOnDataProtectionProviderWithValueProtection()
        {
            IContentHelper sut = CreateSut();

            string base64String = Guid.NewGuid().ToString("D");

            sut.ToValue(base64String);

            _dataProtectionProviderMock.Verify(m => m.CreateProtector(It.Is <string>(value => string.CompareOrdinal(value, "ValueProtection") == 0)), Times.Never);
        }
Example #9
0
        public void ToValue_WhenBase64StringCanBeConvertedToUTF8_ReturnsValue()
        {
            IContentHelper sut = CreateSut();

            string stringValue  = Guid.NewGuid().ToString("D");
            string base64String = BuildStringValueAsBase64String(stringValue);
            string result       = sut.ToValue(base64String);

            Assert.IsNotNull(result);
            Assert.AreEqual(stringValue, result);
        }
Example #10
0
        public void ToValue_WhenByteArrayCanBeConvertedToUTF8_ReturnsValue()
        {
            IContentHelper sut = CreateSut();

            string stringValue = Guid.NewGuid().ToString("D");

            byte[] byteArray = BuildStringValueAsByteArray(stringValue);
            string result    = sut.ToValue(byteArray);

            Assert.IsNotNull(result);
            Assert.AreEqual(stringValue, result);
        }
Example #11
0
        public DashboardViewModel ToDashboardViewModel()
        {
            return(RestoreFromCookie <DashboardViewModel>(DashboardViewModel.CookieName, cookieValue =>
            {
                string memoryCacheKey = _contentHelper.ToValue(cookieValue);
                if (string.IsNullOrWhiteSpace(memoryCacheKey))
                {
                    return null;
                }

                if (_memoryCache.TryGetValue(memoryCacheKey, out byte[] memoryCacheValue) == false)
                {
                    return null;
                }

                return _contentHelper.ToDashboardViewModel(memoryCacheValue);
            }));
Example #12
0
        public void ToValue_WhenByteArrayIsNull_ThrowsArgumentNullException()
        {
            IContentHelper sut = CreateSut();

            sut.ToValue((byte[])null);
        }
Example #13
0
        public void ToValue_WhenBase64StringIsWhiteSpaces_ThrowsArgumentNullException()
        {
            IContentHelper sut = CreateSut();

            sut.ToValue("  ");
        }
Example #14
0
        public void ToValue_WhenBase64StringIsEmpty_ThrowsArgumentNullException()
        {
            IContentHelper sut = CreateSut();

            sut.ToValue(string.Empty);
        }