package com.uwetrottmann.trakt.v2;

import org.apache.oltu.oauth2.client.request.OAuthClientRequest;
import org.apache.oltu.oauth2.client.response.OAuthAccessTokenResponse;
import org.apache.oltu.oauth2.common.exception.OAuthProblemException;
import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
import org.junit.Test;

import java.net.URISyntaxException;

import static org.fest.assertions.api.Assertions.assertThat;

public class AuthTest extends BaseTestCase {

    public static final String TEST_CLIENT_ID = "e683ed71dd4a4afe73ba73151a4645f511b8703464a7807045088c733ef8d634";
    public static final String TEST_CLIENT_SECRET = "21da158feb52479c53936a48b13e4abe94b907908387d47b70710deb2f4a51fa";
    private static final String AUTH_CODE = "";
    public static final String TEST_REDIRECT_URI = "urn:ietf:wg:oauth:2.0:oob";

    @Test
    public void test_getAccessTokenRequest() throws OAuthSystemException {
        OAuthClientRequest request = TraktV2.getAccessTokenRequest(TEST_CLIENT_ID, TEST_CLIENT_SECRET,
                TEST_REDIRECT_URI, AUTH_CODE);
        assertThat(request).isNotNull();
        assertThat(request.getLocationUri()).startsWith(TraktV2.OAUTH2_TOKEN_URL);
        System.out.println("Generated Token Request URI: " + request.getLocationUri());
    }

    @Test
    public void test_getAccessToken() throws OAuthProblemException, OAuthSystemException {
        if (AUTH_CODE.length() == 0) {
            throw new IllegalArgumentException(
                    "Make sure you set a temporary auth code to exchange for an access token");
        }
        OAuthAccessTokenResponse response = TraktV2.getAccessToken(
                TEST_CLIENT_ID, TEST_CLIENT_SECRET, TEST_REDIRECT_URI, AUTH_CODE);
        System.out.println("Retrieved access token: " + response.getAccessToken());
    }

    @Test
    public void test_getAuthorizationRequest() throws OAuthSystemException, URISyntaxException {
        OAuthClientRequest request = TraktV2.getAuthorizationRequest(TEST_CLIENT_ID, TEST_REDIRECT_URI);
        assertThat(request).isNotNull();
        assertThat(request.getLocationUri()).startsWith(TraktV2.OAUTH2_AUTHORIZATION_URL);
        // trakt does not support scopes, so don't send one (server sets default scope)
        assertThat(request.getLocationUri()).doesNotContain("scope");
        System.out.println("Generated Auth Request URI: " + request.getLocationUri());
    }

}

@@ -428,16 +428,140 @@
 tThat;%0A%0A
+/**%0A * This test should NOT be run with the regular test suite. It requires a valid, temporary (!) auth code to be set.%0A */%0A
 public c
@@ -1874,78 +1874,472 @@
     
-System.out.println(%22Retrieved access token: %22 + response.getAccessToke
+assertThat(response.getAccessToken()).isNotEmpty();%0A        assertThat(response.getRefreshToken()).isNull(); // trakt does not supply refresh tokens%0A        System.out.println(%22Retrieved access token: %22 + response.getAccessToken());%0A        System.out.println(%22Retrieved refresh token: %22 + response.getRefreshToken());%0A        System.out.println(%22Retrieved scope: %22 + response.getScope());%0A        System.out.println(%22Retrieved expires in: %22 + response.getExpiresI
 n())
@@ -2883,28 +2883,39 @@
 (%22Ge
-nerated Auth Request
+t an auth code at the following
  URI

Add warning to AuthTest, ensure no refresh token is retrieved.