Authorizes account access.
Inheritance: EndpointBase, IOAuth2Endpoint
Exemplo n.º 1
0
 public void GetAuthorizationUrl_SetState_AreEqual()
 {
     var client = new ImgurClient("abc", "ioa");
     var endpoint = new OAuth2Endpoint(client);
     var expected = "https://api.imgur.com/oauth2/authorize?client_id=abc&response_type=Code&state=test";
     Assert.AreEqual(expected, endpoint.GetAuthorizationUrl(OAuth2ResponseType.Code, "test"));
 }
Exemplo n.º 2
0
 public void GetAuthorizationUrl_SetStateNull_AreEqual()
 {
     var client = new ImgurClient("xyz", "deb");
     var endpoint = new OAuth2Endpoint(client);
     var expected = "https://api.imgur.com/oauth2/authorize?client_id=xyz&response_type=Code&state=";
     Assert.AreEqual(expected, endpoint.GetAuthorizationUrl(OAuth2ResponseType.Code, null));
 }
Exemplo n.º 3
0
        private IOAuth2Token GetOAuth2Token()
        {
            if (_token != null)
                return _token;

            var authentication = new ImgurClient(ClientId, ClientSecret);
            var endpoint = new OAuth2Endpoint(authentication);
            _token = endpoint.GetTokenByRefreshTokenAsync(RefreshToken).Result;
            return _token;
        }
Exemplo n.º 4
0
        public async Task GetTokenByRefreshTokenAsync_SetToken_IsNotNull()
        {
            var authentication = new ImgurClient(ClientId, ClientSecret);
            var endpoint = new OAuth2Endpoint(authentication);
            var token = await endpoint.GetTokenByRefreshTokenAsync(RefreshToken);

            Assert.IsNotNull(token);
            Assert.IsFalse(string.IsNullOrWhiteSpace(token.AccessToken));
            Assert.IsFalse(string.IsNullOrWhiteSpace(token.RefreshToken));
            Assert.IsFalse(string.IsNullOrWhiteSpace(token.AccountId));
            Assert.IsFalse(string.IsNullOrWhiteSpace(token.TokenType));
        }
Exemplo n.º 5
0
        public async Task GetTokenByCodeAsync_ThrowsImgurException()
        {
            var fakeHttpMessageHandler = new FakeHttpMessageHandler();
            var fakeResponse = new HttpResponseMessage(HttpStatusCode.BadRequest)
            {
                Content = new StringContent(OAuth2EndpointResponses.OAuth2TokenResponseError)
            };

            fakeHttpMessageHandler.AddFakeResponse(new Uri("https://api.imgur.com/oauth2/token"), fakeResponse);

            var client = new ImgurClient("123", "1234");
            var endpoint = new OAuth2Endpoint(client, new HttpClient(fakeHttpMessageHandler));
            await endpoint.GetTokenByCodeAsync("12345");
        }
        public async Task GetTokenByCodeAsync_WithClientSecretNull_ThrowsArgumentNullException()
        {
            var client = new ImgurClient("123");
            var endpoint = new OAuth2Endpoint(client);

            var exception =
                await
                    Record.ExceptionAsync(
                        async () => await endpoint.GetTokenByCodeAsync("1234").ConfigureAwait(false))
                        .ConfigureAwait(false);
            Assert.NotNull(exception);
            Assert.IsType<ArgumentNullException>(exception);

            var argNullException = (ArgumentNullException) exception;
            Assert.Equal(argNullException.ParamName, "ClientSecret");
        }
        public async Task GetTokenByCodeAsync_ThrowsImgurException()
        {
            var mockUrl = "https://api.imgur.com/oauth2/token";
            var mockResponse = new HttpResponseMessage(HttpStatusCode.BadRequest)
            {
                Content = new StringContent(MockOAuth2EndpointResponses.OAuth2TokenResponseError)
            };

            var client = new ImgurClient("123", "1234");
            var endpoint = new OAuth2Endpoint(client, new HttpClient(new MockHttpMessageHandler(mockUrl, mockResponse)));

            var exception =
                await
                    Record.ExceptionAsync(
                        async () => await endpoint.GetTokenByCodeAsync("12345").ConfigureAwait(false))
                        .ConfigureAwait(false);
            Assert.NotNull(exception);
            Assert.IsType<ImgurException>(exception);
        }
        public async Task GetTokenByCodeAsync_Equal()
        {
            var mockUrl = "https://api.imgur.com/oauth2/token";
            var mockResponse = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(MockOAuth2EndpointResponses.GetTokenByCode)
            };

            var client = new ImgurClient("123", "1234");
            var endpoint = new OAuth2Endpoint(client, new HttpClient(new MockHttpMessageHandler(mockUrl, mockResponse)));
            var token = await endpoint.GetTokenByCodeAsync("12345").ConfigureAwait(false);

            Assert.Equal("CodeResponse", token.AccessToken);
            Assert.Equal("2132d34234jkljj84ce0c16fjkljfsdfdc70", token.RefreshToken);
            Assert.Equal("bearer", token.TokenType);
            Assert.Equal(2419200, token.ExpiresIn);
            Assert.Equal("Bob", token.AccountUsername);
            Assert.Equal("45344", token.AccountId);
        }
Exemplo n.º 9
0
        public async Task GetTokenByCodeAsync_AreEqual()
        {
            var fakeHttpMessageHandler = new FakeHttpMessageHandler();
            var fakeResponse = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(OAuth2EndpointResponses.OAuth2TokenCodeResponse)
            };

            fakeHttpMessageHandler.AddFakeResponse(new Uri("https://api.imgur.com/oauth2/token"), fakeResponse);

            var client = new ImgurClient("123", "1234");
            var endpoint = new OAuth2Endpoint(client, new HttpClient(fakeHttpMessageHandler));
            var token = await endpoint.GetTokenByCodeAsync("12345");

            Assert.AreEqual("CodeResponse", token.AccessToken);
            Assert.AreEqual("2132d34234jkljj84ce0c16fjkljfsdfdc70", token.RefreshToken);
            Assert.AreEqual("bearer", token.TokenType);
            Assert.AreEqual(2419200, token.ExpiresIn);
            Assert.AreEqual("Bob", token.AccountUsername);
            Assert.AreEqual("45344", token.AccountId);
        }
Exemplo n.º 10
0
 public async Task GetTokenByCodeAsync_SetCodeInvalid_ThrowsImgurException()
 {
     var authentication = new ImgurClient(ClientId, ClientSecret);
     var endpoint = new OAuth2Endpoint(authentication);
     await endpoint.GetTokenByCodeAsync("abc");
 }
Exemplo n.º 11
0
 public async Task GetTokenByCodeAsync_WithCodeNull_ThrowsArgumentNullException()
 {
     var client = new ImgurClient("123", "1234");
     var endpoint = new OAuth2Endpoint(client);
     await endpoint.GetTokenByCodeAsync(null);
 }
Exemplo n.º 12
0
        public async Task GetTokenByRefreshTokenAsync_WithTokenNull_ThrowsArgumentNullException()
        {
            var client = new ImgurClient("123", "1234");
            var endpoint = new OAuth2Endpoint(client);

            var exception =
                await
                    Record.ExceptionAsync(
                        async () => await endpoint.GetTokenByRefreshTokenAsync(null).ConfigureAwait(false))
                        .ConfigureAwait(false);
            Assert.NotNull(exception);
            Assert.IsType<ArgumentNullException>(exception);
        }