use of org.matrix.androidsdk.rest.model.login.AuthParams in project matrix-android-sdk by matrix-org.
the class CommonTestHelper method createAccountAndSync.
/**
* Create an account and a dedicated session
*
* @param context the context
* @param userName the account username
* @param password the password
* @param sessionTestParams parameters for the test
*/
private MXSession createAccountAndSync(Context context, String userName, String password, SessionTestParams sessionTestParams) throws InterruptedException {
final HomeServerConnectionConfig hs = createHomeServerConfig(null);
final LoginRestClient loginRestClient = new LoginRestClient(hs);
final Map<String, Object> params = new HashMap<>();
final RegistrationParams registrationParams = new RegistrationParams();
CountDownLatch lock = new CountDownLatch(1);
// get the registration session id
loginRestClient.register(registrationParams, new TestApiCallback<Credentials>(lock, false) {
@Override
public void onMatrixError(MatrixError e) {
// detect if a parameter is expected
RegistrationFlowResponse registrationFlowResponse = null;
// when a response is not completed the server returns an error message
if ((null != e.mStatus) && (e.mStatus == 401)) {
try {
registrationFlowResponse = JsonUtils.toRegistrationFlowResponse(e.mErrorBodyAsString);
} catch (Exception castExcept) {
}
}
// check if the server response can be casted
if (null != registrationFlowResponse) {
params.put("session", registrationFlowResponse.session);
}
super.onMatrixError(e);
}
});
await(lock);
final String session = (String) params.get("session");
Assert.assertNotNull(session);
registrationParams.username = userName;
registrationParams.password = password;
AuthParams authParams = new AuthParams(LoginRestClient.LOGIN_FLOW_TYPE_DUMMY);
authParams.session = session;
registrationParams.auth = authParams;
lock = new CountDownLatch(1);
loginRestClient.register(registrationParams, new TestApiCallback<Credentials>(lock) {
@Override
public void onSuccess(Credentials credentials) {
params.put("credentials", credentials);
super.onSuccess(credentials);
}
});
await(lock);
Credentials credentials = (Credentials) params.get("credentials");
Assert.assertNotNull(credentials);
hs.setCredentials(credentials);
IMXStore store = new MXFileStore(hs, false, context);
MXDataHandler dataHandler = new MXDataHandler(store, credentials);
dataHandler.setLazyLoadingEnabled(sessionTestParams.getWithLazyLoading());
MXSession mxSession = new MXSession.Builder(hs, dataHandler, context).withLegacyCryptoStore(sessionTestParams.getWithLegacyCryptoStore()).build();
if (sessionTestParams.getWithCryptoEnabled()) {
mxSession.enableCryptoWhenStarting();
}
if (sessionTestParams.getWithInitialSync()) {
syncSession(mxSession, sessionTestParams.getWithCryptoEnabled());
}
return mxSession;
}
Aggregations