RestImpl { void signin(String username, String password, final LoginCallback loginCallback) { checkNotNull(username, "username == null"); checkNotNull(password, "password == null"); checkNotNull(loginCallback, "loginCallback == null"); RequestBody body = new FormBody.Builder() .add("username", username) .add("password", password) .build(); HttpUrl url = requestUrl(baseUrl, "login") .build(); Request request = new Request.Builder() .url(url) .post(body) .build(); Type type = Types.newParameterizedType(RestResult.class, RestToken.class); handleSimpleRestCall(request, type, new ValueCallback<RestResult<RestToken>>() { @Override public void onValue(RestResult<RestToken> data) { RestToken restToken = data.result(); Token token = Token.create(restToken.userId(), restToken.authToken()); if (tokenProvider != null) { tokenProvider.saveToken(token); } loginCallback.onLoginSuccess(token); } }, ERROR_HANDLER(loginCallback)); } RestImpl(OkHttpClient client, Moshi moshi, HttpUrl baseUrl, TokenProvider tokenProvider, Logger logger);   }
@Test public void testSigninShouldFailWithAuthExceptionOn401() { mockServer.expect() .post() .withPath("/api/v1/login") .andReturn(401, "{\"status\": \"error\",\"message\": \"Unauthorized\"}") .once(); rest.signin("user", "password", loginCallback); verify(loginCallback, timeout(DEFAULT_TIMEOUT).only()) .onError(exceptionCaptor.capture()); RocketChatException exception = exceptionCaptor.getValue(); assertThat(exception, is(instanceOf(RocketChatAuthException.class))); assertThat(exception.getMessage(), is(equalTo("Unauthorized"))); }
@Test public void testSigninShouldFailIfNot2xx() { rest.signin("user", "password", loginCallback); verify(loginCallback, timeout(DEFAULT_TIMEOUT).only()) .onError(exceptionCaptor.capture()); RocketChatException exception = exceptionCaptor.getValue(); assertThat(exception, is(instanceOf(RocketChatException.class))); }
@Test(expected = NullPointerException.class) public void testSigninShouldFailWithNullUsername() { rest.signin(null, "password", loginCallback); }
@Test(expected = NullPointerException.class) public void testSigninShouldFailWithNullPassword() { rest.signin("username", null, loginCallback); }
@Test(expected = NullPointerException.class) public void testSigninShouldFailWithNullCallback() { rest.signin("username", "password", null); }
@Test public void tesSigninShouldBeSuccessful() { mockServer.expect() .post() .withPath("/api/v1/login") .andReturn(200, "{\"status\": \"success\",\"data\": {\"authToken\": \"token\",\"userId\": \"userid\"}}") .once(); rest.signin("user", "password", loginCallback); verify(loginCallback, timeout(DEFAULT_TIMEOUT).only()) .onLoginSuccess(tokenCaptor.capture()); Token token = tokenCaptor.getValue(); assertThat(token.userId(), is(equalTo("userid"))); assertThat(token.authToken(), is(equalTo("token"))); assertThat(token.expiresAt(), is(nullValue())); }
@Test public void testSigninShouldFailOnInvalidJson() { mockServer.expect() .post() .withPath("/api/v1/login") .andReturn(200, "NOT A JSON") .once(); rest.signin("user", "password", loginCallback); verify(loginCallback, timeout(DEFAULT_TIMEOUT).only()) .onError(exceptionCaptor.capture()); RocketChatException exception = exceptionCaptor.getValue(); assertThat(exception, is(instanceOf(RocketChatInvalidResponseException.class))); assertThat(exception.getMessage(), is(equalTo("Use JsonReader.setLenient(true) to accept malformed JSON at path $"))); assertThat(exception.getCause(), is(instanceOf(JsonEncodingException.class))); }