2017-04-24 5 views
0

앱 품질을 향상시키기 위해 저는 단위 테스트와 UI 테스트를 테스트하고 있습니다. 내가 Dropbox를 지원하는 데있어 테스트하고 싶다. 테스트하기 전에 Dropbox 계정에 권한을 부여해야한다. (안드로이드 앱에서 사용자가 파일을 저장하고, 이름을 바꾸고, 기본 파일을 읽을 수있다. 루틴).Dropbox Java/Android SDK v2에서 자동으로 인증하는 방법은 무엇입니까?

드롭 박스는 예제와 함께 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()); 

보관 인증 자동으로하지 않고 수동 상호 작용을 할 가능성을? 앱 키/비밀 정보, 계정 이메일/비밀번호를 제공하고 세션에 대해 accessToken을 얻으 려합니다.

추신. Robelectric + Espresso를 사용하지 말고 UI 테스트가 아닌 단위/통합 테스트에 보관하고 싶습니다.

답변

0

내 테스트 템플릿은 다음과 같습니다 (누군가에게 도움이되기를 바랍니다). TODO을 수행하고 적어도 인증 코드를 붙여 넣기 위해 두 번 수동으로 테스트를 실행 한 다음 테스트를 수행 할 수 있습니다. 모든 다음 테스트 호출은 수동으로 수행 할 필요가 없습니다.

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

아니요, 보관 용 계정 API는 앱 인증 절차를 자동화하는 방법을 제공하지 않습니다.

액세스 토큰을 저장하고 재사용 할 수 있으므로 수동으로 한 번만 테스트 계정을 가져 와서 다시 사용할 수 있습니다.

+0

은 앱 패키지와 관련된 액세스 토큰입니까? 내 프로덕션 응용 프로그램에 대해받은 액세스 토큰이 단위 테스트 용으로 수락되는지 궁금합니다. – 4ntoine

+0

Dropbox API 액세스 토큰은 응용 프로그램 사용자 쌍만 식별합니다. 특정 Java/Android 패키지에 묶여 있지 않으므로 여러 위치/환경에서 동일한 액세스 토큰을 사용할 수 있습니다. – Greg