mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 12:23:58 +01:00
With more unit test cases (#3269)
This commit is contained in:
parent
d2fb3b36e7
commit
f27749fa51
7 changed files with 139 additions and 30 deletions
|
|
@ -47,12 +47,4 @@ public class PageEditClient {
|
|||
}
|
||||
}
|
||||
|
||||
public Observable<Integer> addEditTag(long revisionId, String tagName, String reason) {
|
||||
try {
|
||||
return service.addEditTag(String.valueOf(revisionId), tagName, reason, csrfTokenClient.getTokenBlocking())
|
||||
.map(mwPostResponse -> mwPostResponse.getSuccessVal());
|
||||
} catch (Throwable throwable) {
|
||||
return Observable.just(-1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,15 +1,16 @@
|
|||
package fr.free.nrw.commons.auth;
|
||||
|
||||
|
||||
import io.reactivex.Observable;
|
||||
import org.wikipedia.dataclient.Service;
|
||||
import org.wikipedia.dataclient.mwapi.MwPostResponse;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Named;
|
||||
import javax.inject.Singleton;
|
||||
import org.wikipedia.dataclient.Service;
|
||||
import org.wikipedia.dataclient.ServiceFactory;
|
||||
import org.wikipedia.dataclient.WikiSite;
|
||||
import org.wikipedia.dataclient.mwapi.MwPostResponse;
|
||||
|
||||
import io.reactivex.Observable;
|
||||
|
||||
/**
|
||||
* Handler for logout
|
||||
|
|
@ -20,9 +21,8 @@ public class LogoutClient {
|
|||
private final Service service;
|
||||
|
||||
@Inject
|
||||
public LogoutClient(@Named("commons-wikisite")
|
||||
WikiSite commonsWikiSite) {
|
||||
service = ServiceFactory.get(commonsWikiSite);
|
||||
public LogoutClient(@Named("commons-service") Service service) {
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -9,8 +9,6 @@ import android.text.TextUtils;
|
|||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.facebook.drawee.backends.pipeline.Fresco;
|
||||
import com.facebook.imagepipeline.core.ImagePipeline;
|
||||
import org.wikipedia.login.LoginResult;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
|
@ -91,6 +89,10 @@ public class SessionManager {
|
|||
return currentAccount;
|
||||
}
|
||||
|
||||
public boolean doesAccountExist() {
|
||||
return getCurrentAccount() != null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public String getUserName() {
|
||||
Account account = getCurrentAccount();
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
package fr.free.nrw.commons.delete;
|
||||
|
||||
import android.accounts.Account;
|
||||
import android.content.Context;
|
||||
|
||||
import org.wikipedia.util.DateUtil;
|
||||
|
|
@ -16,7 +15,7 @@ import fr.free.nrw.commons.R;
|
|||
import fr.free.nrw.commons.achievements.FeedbackResponse;
|
||||
import fr.free.nrw.commons.auth.SessionManager;
|
||||
import fr.free.nrw.commons.mwapi.OkHttpJsonApiClient;
|
||||
import fr.free.nrw.commons.utils.ViewUtil;
|
||||
import fr.free.nrw.commons.utils.ViewUtilWrapper;
|
||||
import io.reactivex.Single;
|
||||
import timber.log.Timber;
|
||||
|
||||
|
|
@ -26,14 +25,21 @@ public class ReasonBuilder {
|
|||
private SessionManager sessionManager;
|
||||
private OkHttpJsonApiClient okHttpJsonApiClient;
|
||||
private Context context;
|
||||
private ViewUtilWrapper viewUtilWrapper;
|
||||
|
||||
@Inject
|
||||
public ReasonBuilder(Context context,
|
||||
SessionManager sessionManager,
|
||||
OkHttpJsonApiClient okHttpJsonApiClient) {
|
||||
OkHttpJsonApiClient okHttpJsonApiClient,
|
||||
ViewUtilWrapper viewUtilWrapper) {
|
||||
this.context = context;
|
||||
this.sessionManager = sessionManager;
|
||||
this.okHttpJsonApiClient = okHttpJsonApiClient;
|
||||
this.viewUtilWrapper = viewUtilWrapper;
|
||||
}
|
||||
|
||||
public Single<String> getReason(Media media, String reason) {
|
||||
return fetchArticleNumber(media, reason);
|
||||
}
|
||||
|
||||
private String prettyUploadedDate(Media media) {
|
||||
|
|
@ -47,7 +53,7 @@ public class ReasonBuilder {
|
|||
private Single<String> fetchArticleNumber(Media media, String reason) {
|
||||
if (checkAccount()) {
|
||||
return okHttpJsonApiClient
|
||||
.getAchievements(sessionManager.getCurrentAccount().name)
|
||||
.getAchievements(sessionManager.getUserName())
|
||||
.map(feedbackResponse -> appendArticlesUsed(feedbackResponse, media, reason));
|
||||
}
|
||||
return Single.just("");
|
||||
|
|
@ -60,20 +66,14 @@ public class ReasonBuilder {
|
|||
return reason;
|
||||
}
|
||||
|
||||
|
||||
public Single<String> getReason(Media media, String reason) {
|
||||
return fetchArticleNumber(media, reason);
|
||||
}
|
||||
|
||||
/**
|
||||
* check to ensure that user is logged in
|
||||
* @return
|
||||
*/
|
||||
private boolean checkAccount(){
|
||||
Account currentAccount = sessionManager.getCurrentAccount();
|
||||
if(currentAccount == null) {
|
||||
if (!sessionManager.doesAccountExist()) {
|
||||
Timber.d("Current account is null");
|
||||
ViewUtil.showLongToast(context, context.getResources().getString(R.string.user_not_logged_in));
|
||||
viewUtilWrapper.showLongToast(context, context.getResources().getString(R.string.user_not_logged_in));
|
||||
sessionManager.forceLogin(context);
|
||||
return false;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,4 +16,8 @@ public class ViewUtilWrapper {
|
|||
public void showShortToast(Context context, String text) {
|
||||
ViewUtil.showShortToast(context, text);
|
||||
}
|
||||
|
||||
public void showLongToast(Context context, String text) {
|
||||
ViewUtil.showLongToast(context, text);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,46 @@
|
|||
package fr.free.nrw.commons.auth
|
||||
|
||||
import com.nhaarman.mockito_kotlin.verify
|
||||
import io.reactivex.Observable
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.mockito.ArgumentMatchers.anyString
|
||||
import org.mockito.InjectMocks
|
||||
import org.mockito.Mock
|
||||
import org.mockito.Mockito
|
||||
import org.mockito.Mockito.*
|
||||
import org.mockito.MockitoAnnotations
|
||||
import org.wikipedia.dataclient.Service
|
||||
import org.wikipedia.dataclient.mwapi.MwPostResponse
|
||||
import org.wikipedia.dataclient.mwapi.MwQueryResponse
|
||||
import org.wikipedia.dataclient.mwapi.MwQueryResult
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Named
|
||||
|
||||
class LogoutClientTest {
|
||||
|
||||
@Mock @field:[Inject Named("commons-service")]
|
||||
internal var service: Service? = null
|
||||
|
||||
@InjectMocks
|
||||
var logoutClient: LogoutClient? = null
|
||||
|
||||
@Before
|
||||
@Throws(Exception::class)
|
||||
fun setUp() {
|
||||
MockitoAnnotations.initMocks(this)
|
||||
val mwQueryResponse = Mockito.mock(MwQueryResponse::class.java)
|
||||
val mwQueryResult = Mockito.mock(MwQueryResult::class.java)
|
||||
`when`(mwQueryResult!!.csrfToken()).thenReturn("test_token")
|
||||
`when`(mwQueryResponse.query()).thenReturn(mwQueryResult)
|
||||
`when`(service!!.csrfToken)
|
||||
.thenReturn(Observable.just(mwQueryResponse))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun postLogout() {
|
||||
`when`(service!!.postLogout(anyString())).thenReturn(Observable.just(mock(MwPostResponse::class.java)))
|
||||
logoutClient!!.postLogout()
|
||||
verify(service, times(1))!!.csrfToken
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,65 @@
|
|||
package fr.free.nrw.commons.delete
|
||||
|
||||
import android.content.Context
|
||||
import android.content.res.Resources
|
||||
import com.nhaarman.mockito_kotlin.verify
|
||||
import fr.free.nrw.commons.Media
|
||||
import fr.free.nrw.commons.achievements.FeedbackResponse
|
||||
import fr.free.nrw.commons.auth.SessionManager
|
||||
import fr.free.nrw.commons.mwapi.OkHttpJsonApiClient
|
||||
import fr.free.nrw.commons.utils.ViewUtilWrapper
|
||||
import io.reactivex.Single
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.mockito.ArgumentMatchers.anyInt
|
||||
import org.mockito.InjectMocks
|
||||
import org.mockito.Mock
|
||||
import org.mockito.Mockito.*
|
||||
import org.mockito.MockitoAnnotations
|
||||
import java.util.*
|
||||
|
||||
|
||||
class ReasonBuilderTest {
|
||||
|
||||
@Mock
|
||||
internal var sessionManager: SessionManager? = null
|
||||
@Mock
|
||||
internal var okHttpJsonApiClient: OkHttpJsonApiClient? = null
|
||||
@Mock
|
||||
internal var context: Context? = null
|
||||
@Mock
|
||||
internal var viewUtilWrapper: ViewUtilWrapper? = null
|
||||
|
||||
@InjectMocks
|
||||
var reasonBuilder: ReasonBuilder? = null
|
||||
|
||||
@Before
|
||||
@Throws(Exception::class)
|
||||
fun setUp() {
|
||||
MockitoAnnotations.initMocks(this)
|
||||
val resources = mock(Resources::class.java)
|
||||
`when`(resources!!.getString(anyInt())).thenReturn("test")
|
||||
`when`(context!!.resources).thenReturn(resources)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun forceLoginWhenAccountIsNull() {
|
||||
reasonBuilder!!.getReason(mock(Media::class.java), "test")
|
||||
verify(sessionManager, times(1))!!.forceLogin(any(Context::class.java))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun getReason() {
|
||||
`when`(sessionManager?.userName).thenReturn("Testuser")
|
||||
`when`(sessionManager?.doesAccountExist()).thenReturn(true)
|
||||
`when`(okHttpJsonApiClient!!.getAchievements(anyString()))
|
||||
.thenReturn(Single.just(mock(FeedbackResponse::class.java)))
|
||||
|
||||
val media = mock(Media::class.java)
|
||||
`when`(media!!.dateUploaded).thenReturn(Date())
|
||||
|
||||
reasonBuilder!!.getReason(media, "test")
|
||||
verify(sessionManager, times(0))!!.forceLogin(any(Context::class.java))
|
||||
verify(okHttpJsonApiClient, times(1))!!.getAchievements(anyString())
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue