Skip to content

Commit 8b8eb84

Browse files
authored
Moved the CSRF token client over into main commons code (commons-app#5471)
1 parent 3d0e65c commit 8b8eb84

File tree

18 files changed

+252
-37
lines changed

18 files changed

+252
-37
lines changed

app/build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ dependencies {
2525
// Ref: https://docs.gradle.org/7.5/userguide/upgrading_version_5.html#forced_dependencies
2626
//force = true //API 19 support
2727
}
28+
implementation 'com.squareup.retrofit2:retrofit:2.8.1'
29+
implementation "com.squareup.retrofit2:converter-gson:2.8.1"
2830
implementation 'com.squareup.okio:okio:2.2.2'
2931
implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
3032
implementation 'io.reactivex.rxjava2:rxjava:2.2.3'
@@ -99,6 +101,7 @@ dependencies {
99101
testImplementation 'com.facebook.soloader:soloader:0.10.5'
100102
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.3"
101103
debugImplementation("androidx.fragment:fragment-testing:1.6.2")
104+
testImplementation "commons-io:commons-io:2.6"
102105

103106
// Android testing
104107
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0-alpha04'
@@ -134,7 +137,6 @@ dependencies {
134137
implementation "androidx.room:room-rxjava2:$ROOM_VERSION"
135138
kapt "androidx.room:room-compiler:$ROOM_VERSION"
136139
// For Kotlin use kapt instead of annotationProcessor
137-
implementation 'com.squareup.retrofit2:retrofit:2.8.1'
138140
testImplementation "androidx.arch.core:core-testing:2.1.0"
139141

140142
// Pref

app/src/main/java/fr/free/nrw/commons/actions/PageEditClient.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package fr.free.nrw.commons.actions
22

33
import io.reactivex.Observable
44
import io.reactivex.Single
5-
import org.wikipedia.csrf.CsrfTokenClient
5+
import fr.free.nrw.commons.auth.csrf.CsrfTokenClient
66

77
/**
88
* This class acts as a Client to facilitate wiki page editing

app/src/main/java/fr/free/nrw/commons/actions/ThanksClient.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package fr.free.nrw.commons.actions
33
import fr.free.nrw.commons.CommonsApplication
44
import fr.free.nrw.commons.di.NetworkingModule.NAMED_COMMONS_CSRF
55
import io.reactivex.Observable
6-
import org.wikipedia.csrf.CsrfTokenClient
6+
import fr.free.nrw.commons.auth.csrf.CsrfTokenClient
77
import javax.inject.Inject
88
import javax.inject.Named
99
import javax.inject.Singleton

data-client/src/main/java/org/wikipedia/csrf/CsrfTokenClient.java renamed to app/src/main/java/fr/free/nrw/commons/auth/csrf/CsrfTokenClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.wikipedia.csrf;
1+
package fr.free.nrw.commons.auth.csrf;
22

33
import android.text.TextUtils;
44

app/src/main/java/fr/free/nrw/commons/di/NetworkingModule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
import okhttp3.OkHttpClient;
3636
import okhttp3.logging.HttpLoggingInterceptor;
3737
import okhttp3.logging.HttpLoggingInterceptor.Level;
38-
import org.wikipedia.csrf.CsrfTokenClient;
38+
import fr.free.nrw.commons.auth.csrf.CsrfTokenClient;
3939
import org.wikipedia.dataclient.Service;
4040
import org.wikipedia.dataclient.ServiceFactory;
4141
import org.wikipedia.dataclient.WikiSite;

app/src/main/java/fr/free/nrw/commons/notification/NotificationClient.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import fr.free.nrw.commons.notification.models.Notification
55
import fr.free.nrw.commons.notification.models.NotificationType
66
import io.reactivex.Observable
77
import io.reactivex.Single
8-
import org.wikipedia.csrf.CsrfTokenClient
8+
import fr.free.nrw.commons.auth.csrf.CsrfTokenClient
99
import org.wikipedia.dataclient.mwapi.MwQueryResponse
1010
import org.wikipedia.util.DateUtil
1111
import javax.inject.Inject

app/src/main/java/fr/free/nrw/commons/upload/UploadClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import okhttp3.MediaType;
2727
import okhttp3.MultipartBody;
2828
import okhttp3.RequestBody;
29-
import org.wikipedia.csrf.CsrfTokenClient;
29+
import fr.free.nrw.commons.auth.csrf.CsrfTokenClient;
3030
import org.wikipedia.dataclient.mwapi.MwException;
3131
import timber.log.Timber;
3232

app/src/main/java/fr/free/nrw/commons/wikidata/WikiBaseClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import javax.inject.Inject;
1010
import javax.inject.Named;
1111
import javax.inject.Singleton;
12-
import org.wikipedia.csrf.CsrfTokenClient;
12+
import fr.free.nrw.commons.auth.csrf.CsrfTokenClient;
1313
import org.wikipedia.dataclient.mwapi.MwPostResponse;
1414
import timber.log.Timber;
1515

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package fr.free.nrw.commons;
2+
3+
import androidx.annotation.NonNull;
4+
import java.util.List;
5+
import java.util.concurrent.AbstractExecutorService;
6+
import java.util.concurrent.Executor;
7+
import java.util.concurrent.TimeUnit;
8+
import okhttp3.Dispatcher;
9+
import okhttp3.OkHttpClient;
10+
import okhttp3.mockwebserver.MockResponse;
11+
import org.junit.After;
12+
import org.junit.Before;
13+
import org.junit.runner.RunWith;
14+
import org.robolectric.RobolectricTestRunner;
15+
import org.wikipedia.AppAdapter;
16+
import org.wikipedia.dataclient.Service;
17+
import org.wikipedia.dataclient.WikiSite;
18+
import org.wikipedia.json.GsonUtil;
19+
import retrofit2.Retrofit;
20+
import retrofit2.converter.gson.GsonConverterFactory;
21+
22+
@RunWith(RobolectricTestRunner.class)
23+
public abstract class MockWebServerTest {
24+
private OkHttpClient okHttpClient;
25+
private final TestWebServer server = new TestWebServer();
26+
27+
@Before public void setUp() throws Throwable {
28+
AppAdapter.set(new TestAppAdapter());
29+
OkHttpClient.Builder builder = AppAdapter.get().getOkHttpClient(new WikiSite(Service.WIKIPEDIA_URL)).newBuilder();
30+
okHttpClient = builder.dispatcher(new Dispatcher(new ImmediateExecutorService())).build();
31+
server.setUp();
32+
}
33+
34+
@After public void tearDown() throws Throwable {
35+
server.tearDown();
36+
}
37+
38+
@NonNull protected TestWebServer server() {
39+
return server;
40+
}
41+
42+
protected void enqueueFromFile(@NonNull String filename) throws Throwable {
43+
String json = TestFileUtil.readRawFile(filename);
44+
server.enqueue(json);
45+
}
46+
47+
protected void enqueue404() {
48+
final int code = 404;
49+
server.enqueue(new MockResponse().setResponseCode(code).setBody("Not Found"));
50+
}
51+
52+
protected void enqueueMalformed() {
53+
server.enqueue("(╯°□°)╯︵ ┻━┻");
54+
}
55+
56+
protected void enqueueEmptyJson() {
57+
server.enqueue(new MockResponse().setBody("{}"));
58+
}
59+
60+
@NonNull protected OkHttpClient okHttpClient() {
61+
return okHttpClient;
62+
}
63+
64+
@NonNull protected <T> T service(Class<T> clazz) {
65+
return service(clazz, server().getUrl());
66+
}
67+
68+
@NonNull protected <T> T service(Class<T> clazz, @NonNull String url) {
69+
return new Retrofit.Builder()
70+
.baseUrl(url)
71+
.callbackExecutor(new ImmediateExecutor())
72+
.client(okHttpClient)
73+
.addConverterFactory(GsonConverterFactory.create(GsonUtil.getDefaultGson()))
74+
.build()
75+
.create(clazz);
76+
}
77+
78+
public final class ImmediateExecutorService extends AbstractExecutorService {
79+
@Override public void shutdown() {
80+
throw new UnsupportedOperationException();
81+
}
82+
83+
@NonNull @Override public List<Runnable> shutdownNow() {
84+
throw new UnsupportedOperationException();
85+
}
86+
87+
@Override public boolean isShutdown() {
88+
throw new UnsupportedOperationException();
89+
}
90+
91+
@Override public boolean isTerminated() {
92+
throw new UnsupportedOperationException();
93+
}
94+
95+
@Override public boolean awaitTermination(long l, @NonNull TimeUnit timeUnit)
96+
throws InterruptedException {
97+
throw new UnsupportedOperationException();
98+
}
99+
100+
@Override public void execute(@NonNull Runnable runnable) {
101+
runnable.run();
102+
}
103+
}
104+
105+
public class ImmediateExecutor implements Executor {
106+
@Override
107+
public void execute(@NonNull Runnable runnable) {
108+
runnable.run();
109+
}
110+
}
111+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package fr.free.nrw.commons;
2+
3+
import android.annotation.TargetApi;
4+
import androidx.annotation.NonNull;
5+
import java.io.File;
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
import java.io.StringWriter;
9+
import java.nio.charset.StandardCharsets;
10+
import org.apache.commons.io.FileUtils;
11+
import org.apache.commons.io.IOUtils;
12+
13+
public final class TestFileUtil {
14+
private static final String RAW_DIR = "src/test/res/raw/";
15+
16+
public static File getRawFile(@NonNull String rawFileName) {
17+
return new File(RAW_DIR + rawFileName);
18+
}
19+
20+
public static String readRawFile(String basename) throws IOException {
21+
return readFile(getRawFile(basename));
22+
}
23+
24+
@TargetApi(19)
25+
private static String readFile(File file) throws IOException {
26+
return FileUtils.readFileToString(file, StandardCharsets.UTF_8);
27+
}
28+
29+
@TargetApi(19)
30+
public static String readStream(InputStream stream) throws IOException {
31+
StringWriter writer = new StringWriter();
32+
IOUtils.copy(stream, writer, StandardCharsets.UTF_8);
33+
return writer.toString();
34+
}
35+
36+
private TestFileUtil() { }
37+
}

0 commit comments

Comments
 (0)