Ejemplo n.º 1
0
        public async Task AuthenticateAsync_WithInvalidHttpResponse_ShouldReturnDefaultResponseWithNullUser()
        {
            // Setup
            var config = new PlexMediaServerConfig
            {
                PlexAuthenticationRequestUser = new BasicAuth
                {
                    Username = "******",
                    Password = "******"
                },
                ServerAddress = "http://192.168.0.5:32400"
            };
            var         httpService = Substitute.For <IHttpService>();
            var         plexBasicHeadersConstructor = Substitute.For <IHeaderConstructor <PlexBasicRequestHeaders> >();
            var         basicAuthHeadersConstructor = Substitute.For <IHeaderConstructor <BasicAuthRequest> >();
            var         instance                    = new PlexTvAuthenticator(httpService, plexBasicHeadersConstructor, basicAuthHeadersConstructor, config);
            var         username                    = "******";
            var         password                    = "******";
            string      endpointUrl                 = Endpoint.SignIn.Description();
            var         httpRequestMessage          = new HttpRequestMessage(HttpMethod.Post, endpointUrl);
            HttpRequest httpRequest                 = httpRequestMessage.ToHttpRequest();
            var         expectedHttpResponse        = new HttpResponse <UserAuthenticationResponse>();
            var         expectedHttpResponseMessage = new HttpResponseMessage
            {
                Content = new StringContent(JsonConvert.SerializeObject(new object()))
            };

            // Mocks
            httpService
            .RequestAsync <UserAuthenticationResponse>(Arg.Any <HttpRequest>(), Arg.Any <CancellationToken>())
            .Returns((request) =>
            {
                // Assert the details of the request to be correct
                var requestContext = request.Args()[0] as HttpRequest;

                Assert.Equal(httpRequestMessage.RequestUri.AbsoluteUri, requestContext?.Request.RequestUri.AbsoluteUri);
                Assert.Equal(httpRequestMessage.Method, requestContext?.Request.Method);

                return(expectedHttpResponse);
            });

            await expectedHttpResponse.SetResponseAsync(httpRequest.Identifier, expectedHttpResponseMessage, CancellationToken.None);

            // Perform
            UserAuthenticationResponse actual = await instance.AuthenticateAsync(CancellationToken.None);

            // Assert
            httpService
            .Received()
            .RequestAsync <UserAuthenticationResponse>(Arg.Any <HttpRequest>(), CancellationToken.None);

            Assert.Null(actual.User);
        }
Ejemplo n.º 2
0
        public async Task AuthenticateAsync_WithValidParams_ShouldCallRequestAsyncWithValidRequest()
        {
            // Setup
            var config = new PlexMediaServerConfig
            {
                PlexAuthenticationRequestUser = new BasicAuth
                {
                    Username = "******",
                    Password = "******"
                },
                ServerAddress = "http://192.168.0.5:32400"
            };
            var httpService = Substitute.For <IHttpService>();
            var plexBasicHeadersConstructor = Substitute.For <IHeaderConstructor <PlexBasicRequestHeaders> >();
            var basicAuthHeadersConstructor = Substitute.For <IHeaderConstructor <BasicAuthRequest> >();

            plexBasicHeadersConstructor
            .ConstructRequestHeaders(Arg.Any <PlexBasicRequestHeaders>())
            .Returns((request) => new Dictionary <string, string>
            {
                { "X-Plex-Client-Identifier", "test header" }
            });
            basicAuthHeadersConstructor
            .ConstructRequestHeaders(Arg.Any <BasicAuthRequest>())
            .Returns((request) => new Dictionary <string, string>
            {
                { "Authorization", "test header" }
            });
            var         instance           = new PlexTvAuthenticator(httpService, plexBasicHeadersConstructor, basicAuthHeadersConstructor, config);
            var         username           = "******";
            var         password           = "******";
            string      endpointUrl        = Endpoint.SignIn.Description();
            var         httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, endpointUrl);
            HttpRequest httpRequest        = httpRequestMessage.ToHttpRequest();
            var         expectedUser       = new User
            {
                AuthToken = "test token",
                Email     = "test email"
            };
            var expectedResponseBody = new UserAuthenticationResponse
            {
                User = expectedUser
            };
            var expectedHttpResponse        = new HttpResponse <UserAuthenticationResponse>();
            var expectedHttpResponseMessage = new HttpResponseMessage
            {
                Content = new StringContent(JsonConvert.SerializeObject(expectedResponseBody))
            };

            // Mocks
            httpService
            .RequestAsync <UserAuthenticationResponse>(Arg.Any <HttpRequest>(), Arg.Any <CancellationToken>())
            .Returns((request) =>
            {
                // Assert the details of the request to be correct
                var requestContext = request.Args()[0] as HttpRequest;

                Assert.Equal(httpRequestMessage.RequestUri.AbsoluteUri, requestContext?.Request.RequestUri.AbsoluteUri);
                Assert.Equal(httpRequestMessage.Method, requestContext?.Request.Method);
                Assert.True(requestContext?.Request.Headers.Contains("X-Plex-Client-Identifier"));
                Assert.True(requestContext?.Request.Headers.Contains("Authorization"));

                return(expectedHttpResponse);
            });

            await expectedHttpResponse.SetResponseAsync(httpRequest.Identifier, expectedHttpResponseMessage, CancellationToken.None);

            // Perform
            UserAuthenticationResponse actual = await instance.AuthenticateAsync(CancellationToken.None);

            // Assert
            httpService
            .Received()
            .RequestAsync <UserAuthenticationResponse>(Arg.Any <HttpRequest>(), CancellationToken.None);

            Assert.Equal(expectedUser.AuthToken, actual.User.AuthToken);
            Assert.Equal(expectedUser.Email, actual.User.Email);
        }