mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 20:33:53 +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;
|
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 java.util.Objects;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import javax.inject.Named;
|
import javax.inject.Named;
|
||||||
import javax.inject.Singleton;
|
import javax.inject.Singleton;
|
||||||
import org.wikipedia.dataclient.Service;
|
|
||||||
import org.wikipedia.dataclient.ServiceFactory;
|
import io.reactivex.Observable;
|
||||||
import org.wikipedia.dataclient.WikiSite;
|
|
||||||
import org.wikipedia.dataclient.mwapi.MwPostResponse;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handler for logout
|
* Handler for logout
|
||||||
|
|
@ -20,9 +21,8 @@ public class LogoutClient {
|
||||||
private final Service service;
|
private final Service service;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public LogoutClient(@Named("commons-wikisite")
|
public LogoutClient(@Named("commons-service") Service service) {
|
||||||
WikiSite commonsWikiSite) {
|
this.service = service;
|
||||||
service = ServiceFactory.get(commonsWikiSite);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -9,8 +9,6 @@ import android.text.TextUtils;
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
|
|
||||||
import com.facebook.drawee.backends.pipeline.Fresco;
|
|
||||||
import com.facebook.imagepipeline.core.ImagePipeline;
|
|
||||||
import org.wikipedia.login.LoginResult;
|
import org.wikipedia.login.LoginResult;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
@ -91,6 +89,10 @@ public class SessionManager {
|
||||||
return currentAccount;
|
return currentAccount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean doesAccountExist() {
|
||||||
|
return getCurrentAccount() != null;
|
||||||
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public String getUserName() {
|
public String getUserName() {
|
||||||
Account account = getCurrentAccount();
|
Account account = getCurrentAccount();
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
package fr.free.nrw.commons.delete;
|
package fr.free.nrw.commons.delete;
|
||||||
|
|
||||||
import android.accounts.Account;
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
|
||||||
import org.wikipedia.util.DateUtil;
|
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.achievements.FeedbackResponse;
|
||||||
import fr.free.nrw.commons.auth.SessionManager;
|
import fr.free.nrw.commons.auth.SessionManager;
|
||||||
import fr.free.nrw.commons.mwapi.OkHttpJsonApiClient;
|
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 io.reactivex.Single;
|
||||||
import timber.log.Timber;
|
import timber.log.Timber;
|
||||||
|
|
||||||
|
|
@ -26,14 +25,21 @@ public class ReasonBuilder {
|
||||||
private SessionManager sessionManager;
|
private SessionManager sessionManager;
|
||||||
private OkHttpJsonApiClient okHttpJsonApiClient;
|
private OkHttpJsonApiClient okHttpJsonApiClient;
|
||||||
private Context context;
|
private Context context;
|
||||||
|
private ViewUtilWrapper viewUtilWrapper;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public ReasonBuilder(Context context,
|
public ReasonBuilder(Context context,
|
||||||
SessionManager sessionManager,
|
SessionManager sessionManager,
|
||||||
OkHttpJsonApiClient okHttpJsonApiClient) {
|
OkHttpJsonApiClient okHttpJsonApiClient,
|
||||||
|
ViewUtilWrapper viewUtilWrapper) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
this.sessionManager = sessionManager;
|
this.sessionManager = sessionManager;
|
||||||
this.okHttpJsonApiClient = okHttpJsonApiClient;
|
this.okHttpJsonApiClient = okHttpJsonApiClient;
|
||||||
|
this.viewUtilWrapper = viewUtilWrapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Single<String> getReason(Media media, String reason) {
|
||||||
|
return fetchArticleNumber(media, reason);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String prettyUploadedDate(Media media) {
|
private String prettyUploadedDate(Media media) {
|
||||||
|
|
@ -47,7 +53,7 @@ public class ReasonBuilder {
|
||||||
private Single<String> fetchArticleNumber(Media media, String reason) {
|
private Single<String> fetchArticleNumber(Media media, String reason) {
|
||||||
if (checkAccount()) {
|
if (checkAccount()) {
|
||||||
return okHttpJsonApiClient
|
return okHttpJsonApiClient
|
||||||
.getAchievements(sessionManager.getCurrentAccount().name)
|
.getAchievements(sessionManager.getUserName())
|
||||||
.map(feedbackResponse -> appendArticlesUsed(feedbackResponse, media, reason));
|
.map(feedbackResponse -> appendArticlesUsed(feedbackResponse, media, reason));
|
||||||
}
|
}
|
||||||
return Single.just("");
|
return Single.just("");
|
||||||
|
|
@ -60,20 +66,14 @@ public class ReasonBuilder {
|
||||||
return reason;
|
return reason;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public Single<String> getReason(Media media, String reason) {
|
|
||||||
return fetchArticleNumber(media, reason);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* check to ensure that user is logged in
|
* check to ensure that user is logged in
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
private boolean checkAccount(){
|
private boolean checkAccount(){
|
||||||
Account currentAccount = sessionManager.getCurrentAccount();
|
if (!sessionManager.doesAccountExist()) {
|
||||||
if(currentAccount == null) {
|
|
||||||
Timber.d("Current account is null");
|
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);
|
sessionManager.forceLogin(context);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,4 +16,8 @@ public class ViewUtilWrapper {
|
||||||
public void showShortToast(Context context, String text) {
|
public void showShortToast(Context context, String text) {
|
||||||
ViewUtil.showShortToast(context, 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