Пример #1
0
        public async Task PostSession_SessionNotFound_Success()
        {
            // Arrange
            _authenticationRepositoryMock.Setup(m => m.AuthenticateUserAsync(Login, Password, false)).ReturnsAsync(_loginUser);

            var token = Guid.NewGuid().ToString();

            var httpClientProvider = new TestHttpClientProvider(request =>
            {
                if (request.Method == HttpMethod.Get)
                {
                    return(new HttpResponseMessage(HttpStatusCode.NotFound));
                }
                var httpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK);
                httpResponseMessage.Headers.Add("Session-Token", token);
                return(httpResponseMessage);
            });

            var controller = new SessionsController(_authenticationRepositoryMock.Object, httpClientProvider, _logMock.Object);

            // Act
            var result = (ResponseMessageResult)await controller.PostSession(SystemEncryptions.EncodeTo64UTF8(Login), SystemEncryptions.EncodeTo64UTF8(Password));

            // Assert
            Assert.AreEqual(HttpStatusCode.OK, result.Response.StatusCode);
            Assert.AreEqual(token, result.Response.Headers.GetValues("Session-Token").FirstOrDefault());
            var expectedToken = await result.Response.Content.ReadAsStringAsync();

            Assert.AreEqual(expectedToken, token);
        }
Пример #2
0
        public async Task PostSession_AuthenticationException_HttpResponseException()
        {
            // Arrange
            _authenticationRepositoryMock.Setup(m => m.AuthenticateUserAsync(Login, Password, false))
            .Throws(new AuthenticationException("Invalid username or password"));

            var controller = new SessionsController(_authenticationRepositoryMock.Object, _httpClientProvider, _logMock.Object)
            {
                Request = new HttpRequestMessage()
            };

            // Act
            await controller.PostSession(SystemEncryptions.EncodeTo64UTF8(Login), SystemEncryptions.EncodeTo64UTF8(Password), true);
        }
Пример #3
0
        public async Task PostSession_ArgumentNullException_BadRequestResult()
        {
            // Arrange
            _authenticationRepositoryMock.Setup(m => m.AuthenticateUserAsync(Login, Password, false))
            .Throws(new ArgumentNullException());

            var controller = new SessionsController(_authenticationRepositoryMock.Object, _httpClientProvider, _logMock.Object);

            // Act
            IHttpActionResult result = await controller.PostSession(SystemEncryptions.EncodeTo64UTF8(Login), SystemEncryptions.EncodeTo64UTF8(Password), true);

            // Assert
            Assert.IsInstanceOfType(result, typeof(BadRequestResult));
        }
Пример #4
0
        public async Task PostSession_ServerError_InternalServerErrorResult()
        {
            // Arrange
            _authenticationRepositoryMock.Setup(m => m.AuthenticateUserAsync(Login, Password, false)).ReturnsAsync(_loginUser);

            var httpClientProvider = new TestHttpClientProvider(request => new HttpResponseMessage(HttpStatusCode.NotFound));

            var controller = new SessionsController(_authenticationRepositoryMock.Object, httpClientProvider, _logMock.Object);

            // Act
            IHttpActionResult result = await controller.PostSession(SystemEncryptions.EncodeTo64UTF8(Login), SystemEncryptions.EncodeTo64UTF8(Password), true);

            // Assert
            Assert.IsInstanceOfType(result, typeof(InternalServerErrorResult));
        }
Пример #5
0
        public async Task PostSession_FormatException_BadRequestResult()
        {
            // Arrange
            var controller = new SessionsController(_authenticationRepositoryMock.Object, _httpClientProvider, _logMock.Object)
            {
                Request = new HttpRequestMessage()
            };

            // Act
            try
            {
                await controller.PostSession(Login, Password, true);
            }
            catch (HttpResponseException ex)
            {
                Assert.IsTrue(ex.Response.StatusCode == HttpStatusCode.Unauthorized);
                return;
            }
            // Assert
            Assert.IsTrue(false);
        }