2017-04-24 12 views
0

アプリの品質を向上させるために、私はテストに取り組んでいます:ユニットテストとUIテスト。私はアプリケーションでDropboxのサポートを持っているので、私はそれをテストしたいと思うと私はDropboxのアカウントをテストする前に認証する必要があります(私のアンドロイドアプリでユーザーはファイルを保存することができます、ルーチン)。Dropbox Java/Android SDK v2で自動的に認証する方法は?

Dropboxのは例でJava/Android SDK v2を提供しますが、でもcommand-line toolいくつかの手動アクションが必要です - URLを選択し、アカウントを持つ開いているブラウザアプリを:

// Run through Dropbox API authorization process 
     DbxRequestConfig requestConfig = new DbxRequestConfig("examples-authorize"); 
     DbxWebAuth webAuth = new DbxWebAuth(requestConfig, appInfo); 
     DbxWebAuth.Request webAuthRequest = DbxWebAuth.newRequestBuilder() 
      .withNoRedirect() 
      .build(); 

     String authorizeUrl = webAuth.authorize(webAuthRequest); 
     System.out.println("1. Go to " + authorizeUrl); 
     System.out.println("2. Click \"Allow\" (you might have to log in first)."); 
     System.out.println("3. Copy the authorization code."); 
     System.out.print("Enter the authorization code here: "); 

     String code = new BufferedReader(new InputStreamReader(System.in)).readLine(); 
     if (code == null) { 
      System.exit(1); return; 
     } 
     code = code.trim(); 

     DbxAuthFinish authFinish; 
     try { 
      authFinish = webAuth.finishFromCode(code); 
     } catch (DbxException ex) { 
      System.err.println("Error in DbxWebAuth.authorize: " + ex.getMessage()); 
      System.exit(1); return; 
     } 

     System.out.println("Authorization complete."); 
     System.out.println("- User ID: " + authFinish.getUserId()); 
     System.out.println("- Access Token: " + authFinish.getAccessToken()); 

手動操作なしで自動的にDropboxの認証を行うための任意の可能性は?私は、アプリケーションの鍵/秘密、アカウントの電子メール/パスワードを提供し、セッションのためにaccessTokenを得ると期待しています。

PS。私はRobelectric + Espressoの使用を避け、UIテストではなくユニット/統合テストでそれを保持したいと思います。

答えて

0

ここに私のテストテンプレート(私はそれが誰かを助けることを望む)です。 TODOを実行し、少なくとも認証コードとアクセストークンを貼り付けるために手動で2回テストを実行してから、テストを実行する必要があります。次のテスト呼び出しは、手動で何もする必要はありません。

public class DropboxFileSystemTest { 

    // credentials 
    private static final String APP_KEY = ""; // TODO : paste your app key 
    private static final String APP_SECRET = ""; // TODO : paste your app secret 

    // do run the test and follow the instructions 
    private static final String accountEmail = "[email protected]"; // TODO : paste your test Dropbox account 
    private static String authCode; // TODO : run the test and paste auth code 
    private static String accessToken // TODO : run the test and paste access 

    private DbxAppInfo appInfo = new DbxAppInfo(APP_KEY, APP_SECRET); 

    // Run through Dropbox API authorization process 
    private DbxRequestConfig requestConfig = new DbxRequestConfig(DropboxFileSystemTest.class.getSimpleName()); 
    private DbxWebAuth webAuth = new DbxWebAuth(requestConfig, appInfo); 

    private void startAuth() throws IOException { 
     DbxWebAuth.Request webAuthRequest = DbxWebAuth.newRequestBuilder() 
      .withNoRedirect() 
      .build(); 

     String authorizeUrl = webAuth.authorize(webAuthRequest); 
     System.out.println("1. Go to " + authorizeUrl); 
     System.out.println("2. Click \"Allow\" (you might have to log in first). WARNING: log-in to " + accountEmail); 
     System.out.println("3. Copy the authorization code."); 
     System.out.println("4. Paste the authorization code to this test `this.authCode` value"); 
     System.out.println("5. Re-run the test"); 
    } 

    private DbxClientV2 client; // to be used for the requests 

    private void initWithAccessToken() { 
     DbxRequestConfig config = new DbxRequestConfig(UUID.randomUUID().toString()); 
     client = new DbxClientV2(config, accessToken); 
    } 

    private void initAndVerifyAccount() throws DbxException { 
     initWithAccessToken(); 

     // check expected account (trying to prevent user account to be wiped out) 
     DbxClientV2 client = DbxClientHolder.get().getClient(); 
     FullAccount account = client.users().getCurrentAccount(); 
     if (!account.getEmail().equals(accountEmail)) 
      throw new RuntimeException("Wrong account: current is " + account.getEmail() + ", but " + accountEmail + " is expected"); 
    } 

    private void clearFileSystem() throws FileSystemException { 
     // TODO : clear Dropbox file system 
    } 

    @Before 
    public void setUp() throws IOException, FileSystemException, DbxException { 
     auth(); 
     clearFileSystem(); 
    } 

    private void finishAuth() { 
     DbxAuthFinish authFinish; 
     try { 
      authFinish = webAuth.finishFromCode(authCode); 
     } catch (DbxException ex) { 
      System.err.println("Error in DbxWebAuth.authorize: " + ex.getMessage()); 
      System.exit(1); return; 
     } 

     System.out.println("Authorization complete."); 
     System.out.println("- User ID: " + authFinish.getUserId()); 
     System.out.println("- Access Token: " + authFinish.getAccessToken()); 
     System.out.println(); 

     System.out.println("1. Copy the access token"); 
     System.out.println("2. Paste the access token to this test `this.accessToken` value"); 
     System.out.println("3. Re-run the test"); 

     accessToken = authFinish.getAccessToken(); 
    } 

    private void auth() throws IOException, FileSystemException, DbxException { 
     if (accessToken == null) { 
      if (authCode != null) { 
       finishAuth(); 
       throw new RuntimeException("Manual actions required: copy-paste access token"); 
      } else { 
       startAuth(); 
       throw new RuntimeException("Manual actions required: copy-paste authCode"); 
      } 
     } else { 
      initAndVerifyAccount(); 
     } 
    } 

    @After 
    public void tearDown() throws FileSystemException { 
     if (client != null) { 
      clearFileSystem(); 
     } 
    } 

    @Test 
    public void testSmth() { 
     // TODO : write your test using `this.client` for requests 
    } 
1

いいえ、Dropbox APIではアプリの承認フローを自動化する手段はありません。

アクセストークンを保存して再利用することができるので、手動でテストアカウント用のトークンを1回取得してから再利用することができます。

+0

はアプリパッケージに関連するアクセストークンですか?プロダクション用に受け取ったアクセストークンがユニットテスト用に受け入れられるかどうか疑問に思う – 4ntoine

+0

Dropbox APIアクセストークンは、アプリとユーザーのペアを識別するだけです。特定のJava/Androidパッケージには結びついていないので、複数の場所/環境から同じアクセストークンを使用できます。 – Greg

関連する問題