Esempio n. 1
0
        public async Task Login_WithInvalidCredentials_ReturnsErrorResponse()
        {
            // arrange
            var mockFactory            = new Mock <IHttpClientFactory>();
            var mockHttpMessageHandler = new Mock <HttpMessageHandler>();

            mockHttpMessageHandler.Protected()
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>()).ReturnsAsync(
                new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.Unauthorized
            });


            var client = new HttpClient(mockHttpMessageHandler.Object);

            mockFactory.Setup(_ => _.CreateClient(It.IsAny <string>())).Returns(client);
            var geoCosyClient = new GeoCosyClient(mockFactory.Object);

            // act
            var response = await geoCosyClient.LoginAsync("username", "password");

            // assert
            response.Data.ShouldBeNull();
            response.Errors.ShouldContain(x => x.ErrorCode == ErrorCode.AuthenticationFailed);
        }
Esempio n. 2
0
        public async Task LoginAync_WithValidCredentials_ShouldReturnToken()
        {
            // arrange
            var mockFactory            = new Mock <IHttpClientFactory>();
            var mockHttpMessageHandler = new Mock <HttpMessageHandler>();
            var mockContentResponse    = new CosyLoginResponse()
            {
                Token = "xyz"
            };

            mockHttpMessageHandler.Protected()
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>()).ReturnsAsync(
                new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent(JsonConvert.SerializeObject(mockContentResponse), Encoding.UTF8, "application/json"),
            });


            var client = new HttpClient(mockHttpMessageHandler.Object);

            mockFactory.Setup(_ => _.CreateClient(It.IsAny <string>())).Returns(client);
            var geoCosyClient = new GeoCosyClient(mockFactory.Object);

            // act
            var response = await geoCosyClient.LoginAsync("username", "password");

            // assert
            response.Data.ShouldNotBeNull();
            response.Data.Token.ShouldBe("xyz");
        }
Esempio n. 3
0
        public async Task SetMode_WithValidParametersParameters_SetsMode(CosyMode mode)
        {
            // arrange
            var mockFactory            = new Mock <IHttpClientFactory>();
            var mockHttpMessageHandler = new Mock <HttpMessageHandler>();
            var mockContentResponse    = GetFakeCosyDeviceReponse();

            mockHttpMessageHandler.Protected()
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>()).ReturnsAsync(
                new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent(
                    JsonConvert.SerializeObject(mockContentResponse),
                    Encoding.UTF8,
                    "application/json"),
            });


            var client = new HttpClient(mockHttpMessageHandler.Object);

            mockFactory.Setup(_ => _.CreateClient(It.IsAny <string>())).Returns(client);
            var geoCosyClient = new GeoCosyClient(mockFactory.Object);

            // act
            var response = await geoCosyClient.SetMode("token", "device-id", mode, 60);

            // assert
            response.Data.ShouldBe(true);
        }
Esempio n. 4
0
        public async Task GetSystems_WithValidResponse_ReturnsSystems()
        {
            // arrange
            var mockFactory            = new Mock <IHttpClientFactory>();
            var mockHttpMessageHandler = new Mock <HttpMessageHandler>();
            var mockContentResponse    = GetFakeCosyDeviceReponse();

            mockHttpMessageHandler.Protected()
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>()).ReturnsAsync(
                new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent(JsonConvert.SerializeObject(mockContentResponse), Encoding.UTF8, "application/json"),
            });


            var client = new HttpClient(mockHttpMessageHandler.Object);

            mockFactory.Setup(_ => _.CreateClient(It.IsAny <string>())).Returns(client);
            var geoCosyClient = new GeoCosyClient(mockFactory.Object);

            // act
            var response = await geoCosyClient.GetSystems("incorrect-token", false);

            // assert
            response.Data.ShouldNotBeNull();
            response.Data.SystemDetails[0].ShouldBeEquivalentTo(mockContentResponse.SystemDetails[0]);
            response.Data.SystemRoles[0].ShouldBeEquivalentTo(mockContentResponse.SystemRoles[0]);
        }
Esempio n. 5
0
        public async Task GetSystems_WithInvalidToken_ReturnsErrorResponse()
        {
            // arrange
            var mockFactory            = new Mock <IHttpClientFactory>();
            var mockHttpMessageHandler = new Mock <HttpMessageHandler>();

            mockHttpMessageHandler.Protected()
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>()).ReturnsAsync(
                new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.Forbidden
            });


            var client = new HttpClient(mockHttpMessageHandler.Object);

            mockFactory.Setup(_ => _.CreateClient(It.IsAny <string>())).Returns(client);
            var geoCosyClient = new GeoCosyClient(mockFactory.Object);

            // act
            var response = await geoCosyClient.GetSystems("incorrect-token", false);

            // assert
            response.Data.ShouldBeNull();
            response.Errors.ShouldContain(x => x.ErrorCode == ErrorCode.NotAllowed);
        }
Esempio n. 6
0
        public async Task Login_WithSomeOtherException_ThrowsTheException()
        {
            // arrange
            var mockFactory            = new Mock <IHttpClientFactory>();
            var mockHttpMessageHandler = new Mock <HttpMessageHandler>();

            mockHttpMessageHandler.Protected()
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>()).ReturnsAsync(
                new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.BadGateway
            });


            var client = new HttpClient(mockHttpMessageHandler.Object);

            mockFactory.Setup(_ => _.CreateClient(It.IsAny <string>())).Returns(client);
            var geoCosyClient = new GeoCosyClient(mockFactory.Object);

            // act / assert
            Should.Throw <Exception>(async() => await geoCosyClient.LoginAsync("username", "password"));
        }