use of com.microsoft.appcenter.http.HttpClientNetworkStateHandler in project AppCenter-SDK-Android by Microsoft.
the class DistributeBeforeApiSuccessTest method storeBetterEncryptedToken.
@Test
public void storeBetterEncryptedToken() throws Exception {
/* Mock we already have token. */
when(PreferencesStorage.getString(PREFERENCE_KEY_UPDATE_TOKEN)).thenReturn("some encrypted token");
when(mCryptoUtils.decrypt(eq("some encrypted token"), anyBoolean())).thenReturn(new CryptoUtils.DecryptedData("some token", "some better encrypted token"));
HttpClientNetworkStateHandler httpClient = mock(HttpClientNetworkStateHandler.class);
whenNew(HttpClientNetworkStateHandler.class).withAnyArguments().thenReturn(httpClient);
HashMap<String, String> headers = new HashMap<>();
headers.put(DistributeConstants.HEADER_API_TOKEN, "some token");
/* Trigger call. */
start();
Distribute.getInstance().onActivityResumed(mock(Activity.class));
verify(httpClient).callAsync(anyString(), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
/* Verify storage was updated with new encrypted value. */
verifyStatic();
PreferencesStorage.putString(PREFERENCE_KEY_UPDATE_TOKEN, "some better encrypted token");
}
use of com.microsoft.appcenter.http.HttpClientNetworkStateHandler in project AppCenter-SDK-Android by Microsoft.
the class DistributeBeforeApiSuccessTest method willNotReportReleaseInstallForPrivateGroupWithoutStoredReleaseHash.
@Test
public void willNotReportReleaseInstallForPrivateGroupWithoutStoredReleaseHash() throws Exception {
when(PreferencesStorage.getString(PREFERENCE_KEY_UPDATE_TOKEN)).thenReturn("some encrypted token");
when(mCryptoUtils.decrypt(eq("some encrypted token"), anyBoolean())).thenReturn(new CryptoUtils.DecryptedData("some token", "some better encrypted token"));
/* Mock httpClient. */
HttpClientNetworkStateHandler httpClient = mock(HttpClientNetworkStateHandler.class);
whenNew(HttpClientNetworkStateHandler.class).withAnyArguments().thenReturn(httpClient);
HashMap<String, String> headers = new HashMap<>();
headers.put(DistributeConstants.HEADER_API_TOKEN, "some token");
/* Trigger call. */
start();
Distribute.getInstance().onActivityResumed(mActivity);
ArgumentMatcher<String> urlArg = new ArgumentMatcher<String>() {
@Override
public boolean matches(Object argument) {
return argument.toString().matches("^https://.*?/sdk/apps/a/releases/latest\\?release_hash=" + TEST_HASH + "$");
}
};
verify(httpClient).callAsync(argThat(urlArg), eq("GET"), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
}
use of com.microsoft.appcenter.http.HttpClientNetworkStateHandler in project AppCenter-SDK-Android by Microsoft.
the class DistributeBeforeApiSuccessTest method reportReleaseInstallForPrivateGroupWhenReleaseHashesMatch.
@Test
public void reportReleaseInstallForPrivateGroupWhenReleaseHashesMatch() throws Exception {
final String distributionGroupId = "fake-distribution-id";
when(mMobileCenterPreferencesStorage.getString(PREFERENCE_KEY_UPDATE_TOKEN, null)).thenReturn("some token MC");
when(mMobileCenterPreferencesStorage.getString(PREFERENCE_KEY_DISTRIBUTION_GROUP_ID, null)).thenReturn(distributionGroupId);
when(PreferencesStorage.getString(PREFERENCE_KEY_DOWNLOADED_RELEASE_HASH)).thenReturn(TEST_HASH);
when(PreferencesStorage.getInt(PREFERENCE_KEY_DOWNLOADED_RELEASE_ID)).thenReturn(4);
/* Mock install id from AppCenter. */
final UUID installId = UUID.randomUUID();
when(mAppCenterFuture.get()).thenReturn(installId);
when(AppCenter.getInstallId()).thenReturn(mAppCenterFuture);
/* Mock httpClient. */
HttpClientNetworkStateHandler httpClient = mock(HttpClientNetworkStateHandler.class);
whenNew(HttpClientNetworkStateHandler.class).withAnyArguments().thenReturn(httpClient);
HashMap<String, String> headers = new HashMap<>();
headers.put(DistributeConstants.HEADER_API_TOKEN, "some token MC");
/* Primary storage will be missing data. */
start();
Distribute.getInstance().onActivityResumed(mActivity);
ArgumentMatcher<String> urlArg = new ArgumentMatcher<String>() {
@Override
public boolean matches(Object argument) {
return argument.toString().matches("^https://.*?/sdk/apps/a/releases/latest\\?release_hash=" + TEST_HASH + "&distribution_group_id=" + distributionGroupId + "&downloaded_release_id=4$");
}
};
verify(httpClient).callAsync(argThat(urlArg), eq("GET"), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
}
use of com.microsoft.appcenter.http.HttpClientNetworkStateHandler in project AppCenter-SDK-Android by Microsoft.
the class DistributeBeforeApiSuccessTest method storePrivateRedirectionBeforeStart.
@Test
public void storePrivateRedirectionBeforeStart() throws Exception {
/* Setup mock. */
when(PreferencesStorage.getString(PREFERENCE_KEY_REQUEST_ID)).thenReturn("r");
HttpClientNetworkStateHandler httpClient = mock(HttpClientNetworkStateHandler.class);
whenNew(HttpClientNetworkStateHandler.class).withAnyArguments().thenReturn(httpClient);
/* Store token before start, start in background, no storage access. */
Distribute.getInstance().storeRedirectionParameters("r", "g", "some token");
start();
verifyStatic(never());
PreferencesStorage.putString(anyString(), anyString());
verifyStatic(never());
PreferencesStorage.remove(anyString());
/* Unlock the processing by going into foreground. */
Distribute.getInstance().onActivityResumed(mock(Activity.class));
verifyStatic();
PreferencesStorage.putString(PREFERENCE_KEY_UPDATE_TOKEN, "some token");
verifyStatic();
PreferencesStorage.putString(PREFERENCE_KEY_DISTRIBUTION_GROUP_ID, "g");
verifyStatic();
PreferencesStorage.remove(PREFERENCE_KEY_REQUEST_ID);
verifyStatic();
PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_ID);
verifyStatic();
PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
verify(mDistributeInfoTracker).updateDistributionGroupId("g");
HashMap<String, String> headers = new HashMap<>();
headers.put(DistributeConstants.HEADER_API_TOKEN, "some token");
verify(httpClient).callAsync(anyString(), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
}
use of com.microsoft.appcenter.http.HttpClientNetworkStateHandler in project AppCenter-SDK-Android by Microsoft.
the class DistributeBeforeApiSuccessTest method happyPathUntilHangingCallWithToken.
@Test
public void happyPathUntilHangingCallWithToken() throws Exception {
/* Setup mock. */
HttpClientNetworkStateHandler httpClient = mock(HttpClientNetworkStateHandler.class);
whenNew(HttpClientNetworkStateHandler.class).withAnyArguments().thenReturn(httpClient);
UUID requestId = UUID.randomUUID();
when(UUIDUtils.randomUUID()).thenReturn(requestId);
when(PreferencesStorage.getString(PREFERENCE_KEY_REQUEST_ID)).thenReturn(requestId.toString());
when(mPackageManager.getPackageInfo(DistributeUtils.TESTER_APP_PACKAGE_NAME, 0)).thenThrow(new PackageManager.NameNotFoundException());
/* Mock install id from AppCenter. */
UUID installId = UUID.randomUUID();
when(mAppCenterFuture.get()).thenReturn(installId);
when(AppCenter.getInstallId()).thenReturn(mAppCenterFuture);
/* Start and resume: open browser. */
start();
Distribute.getInstance().onActivityResumed(mActivity);
verifyStatic();
String url = DistributeConstants.DEFAULT_INSTALL_URL;
url += String.format(UPDATE_SETUP_PATH_FORMAT, "a");
url += "?" + PARAMETER_RELEASE_HASH + "=" + TEST_HASH;
url += "&" + PARAMETER_REDIRECT_ID + "=" + mContext.getPackageName();
url += "&" + PARAMETER_REDIRECT_SCHEME + "=" + "appcenter";
url += "&" + PARAMETER_REQUEST_ID + "=" + requestId.toString();
url += "&" + PARAMETER_PLATFORM + "=" + PARAMETER_PLATFORM_VALUE;
url += "&" + PARAMETER_ENABLE_UPDATE_SETUP_FAILURE_REDIRECT_KEY + "=" + "true";
url += "&" + PARAMETER_INSTALL_ID + "=" + installId.toString();
BrowserUtils.openBrowser(url, mActivity);
verifyStatic();
PreferencesStorage.putString(PREFERENCE_KEY_REQUEST_ID, requestId.toString());
/* If browser already opened, activity changed must not recall it. */
Distribute.getInstance().onActivityPaused(mActivity);
Distribute.getInstance().onActivityResumed(mActivity);
verifyStatic();
BrowserUtils.openBrowser(url, mActivity);
verifyStatic();
PreferencesStorage.putString(PREFERENCE_KEY_REQUEST_ID, requestId.toString());
/* Store token. */
Distribute.getInstance().storeRedirectionParameters(requestId.toString(), "g", "some token");
/* Verify behavior. */
verifyStatic();
PreferencesStorage.putString(PREFERENCE_KEY_UPDATE_TOKEN, "some token");
verifyStatic();
PreferencesStorage.putString(PREFERENCE_KEY_DISTRIBUTION_GROUP_ID, "g");
verifyStatic();
PreferencesStorage.remove(PREFERENCE_KEY_REQUEST_ID);
verifyStatic();
PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_ID);
verifyStatic();
PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
verify(mDistributeInfoTracker).updateDistributionGroupId("g");
HashMap<String, String> headers = new HashMap<>();
headers.put(DistributeConstants.HEADER_API_TOKEN, "some token");
verify(httpClient).callAsync(argThat(new ArgumentMatcher<String>() {
@Override
public boolean matches(Object argument) {
return argument.toString().startsWith(DistributeConstants.DEFAULT_API_URL);
}
}), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
/* If call already made, activity changed must not recall it. */
Distribute.getInstance().onActivityPaused(mActivity);
Distribute.getInstance().onActivityResumed(mActivity);
/* Verify behavior. */
verifyStatic();
PreferencesStorage.putString(PREFERENCE_KEY_UPDATE_TOKEN, "some token");
verifyStatic();
PreferencesStorage.putString(PREFERENCE_KEY_DISTRIBUTION_GROUP_ID, "g");
verifyStatic();
PreferencesStorage.remove(PREFERENCE_KEY_REQUEST_ID);
verifyStatic();
PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_ID);
verifyStatic();
PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
verify(mDistributeInfoTracker).updateDistributionGroupId("g");
verify(httpClient).callAsync(argThat(new ArgumentMatcher<String>() {
@Override
public boolean matches(Object argument) {
return argument.toString().startsWith(DistributeConstants.DEFAULT_API_URL);
}
}), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
/* Call is still in progress. If we restart app, nothing happens we still wait. */
restartResumeLauncher(mActivity);
/* Verify behavior not changed. */
verifyStatic();
BrowserUtils.openBrowser(url, mActivity);
verifyStatic();
PreferencesStorage.putString(PREFERENCE_KEY_UPDATE_TOKEN, "some token");
verifyStatic();
PreferencesStorage.putString(PREFERENCE_KEY_DISTRIBUTION_GROUP_ID, "g");
verifyStatic();
PreferencesStorage.remove(PREFERENCE_KEY_REQUEST_ID);
verifyStatic();
PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_ID);
verifyStatic();
PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
verify(mDistributeInfoTracker).updateDistributionGroupId("g");
verify(httpClient).callAsync(argThat(new ArgumentMatcher<String>() {
@Override
public boolean matches(Object argument) {
return argument.toString().startsWith(DistributeConstants.DEFAULT_API_URL);
}
}), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
/* If process is restarted, a new call will be made. Need to mock storage for that. */
when(PreferencesStorage.getString(PREFERENCE_KEY_DISTRIBUTION_GROUP_ID)).thenReturn("g");
when(PreferencesStorage.getString(PREFERENCE_KEY_UPDATE_TOKEN)).thenReturn("some token");
restartProcessAndSdk();
Distribute.getInstance().onActivityResumed(mActivity);
verify(httpClient, times(2)).callAsync(argThat(new ArgumentMatcher<String>() {
@Override
public boolean matches(Object argument) {
return argument.toString().startsWith(DistributeConstants.DEFAULT_API_URL);
}
}), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
}
Aggregations