mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-27 12:53:55 +01:00
Updated isUserBlocked method to check the expiration time instead of just if an expiration time exists. Updated tests accordingly
This commit is contained in:
parent
39bf214c57
commit
47bffa1b21
2 changed files with 42 additions and 3 deletions
|
|
@ -37,6 +37,7 @@ import java.util.Collections;
|
|||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
import fr.free.nrw.commons.BuildConfig;
|
||||
|
|
@ -630,8 +631,17 @@ public class ApacheHttpClientMediaWikiApi implements MediaWikiApi {
|
|||
.param("uiprop", "blockinfo")
|
||||
.get();
|
||||
if(result != null) {
|
||||
// the blockexpiry field will only be present if the user is currently blocked
|
||||
userBlocked = !result.getString("/api/query/userinfo/@blockexpiry").isEmpty();
|
||||
String blockEnd = result.getString("/api/query/userinfo/@blockexpiry");
|
||||
if(blockEnd.equals("infinite"))
|
||||
{
|
||||
userBlocked = true;
|
||||
}
|
||||
else if (!blockEnd.isEmpty()) {
|
||||
Date endDate = parseMWDate(blockEnd);
|
||||
Date current = new Date();
|
||||
userBlocked = endDate.after(current);
|
||||
}
|
||||
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
|
|
@ -642,10 +652,12 @@ public class ApacheHttpClientMediaWikiApi implements MediaWikiApi {
|
|||
|
||||
private Date parseMWDate(String mwDate) {
|
||||
SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ENGLISH); // Assuming MW always gives me UTC
|
||||
isoFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
|
||||
try {
|
||||
return isoFormat.parse(mwDate);
|
||||
} catch (ParseException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ import org.robolectric.RobolectricTestRunner
|
|||
import org.robolectric.RuntimeEnvironment
|
||||
import org.robolectric.annotation.Config
|
||||
import java.net.URLDecoder
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.*
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
|
|
@ -243,7 +244,11 @@ class ApacheHttpClientMediaWikiApiTest {
|
|||
|
||||
@Test
|
||||
fun isUserBlockedForTimeBlockedUser() {
|
||||
server.enqueue(MockResponse().setBody("<?xml version=\"1.0\"?><api><query><userinfo id=\"1000\" name=\"testusername\" blockid=\"3000\" blockedby=\"blockerusername\" blockedbyid=\"1001\" blockreason=\"testing\" blockedtimestamp=\"2018-05-24T15:32:09Z\" blockexpiry=\"2014-09-18T12:34:56Z\"></userinfo></query></api>"))
|
||||
val currentDate = Date()
|
||||
val expiredDate = Date(currentDate.time + 10000)
|
||||
val dateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")
|
||||
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"))
|
||||
server.enqueue(MockResponse().setBody("<?xml version=\"1.0\"?><api><query><userinfo id=\"1000\" name=\"testusername\" blockid=\"3000\" blockedby=\"blockerusername\" blockedbyid=\"1001\" blockreason=\"testing\" blockedtimestamp=\"2018-05-24T15:32:09Z\" blockexpiry=\"" + dateFormat.format(expiredDate) + "\"></userinfo></query></api>"))
|
||||
|
||||
val result = testObject.isUserBlocked();
|
||||
|
||||
|
|
@ -259,6 +264,28 @@ class ApacheHttpClientMediaWikiApiTest {
|
|||
assertTrue(result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun isUserBlockedForExpiredBlockedUser() {
|
||||
val currentDate = Date()
|
||||
val expiredDate = Date(currentDate.time - 10000)
|
||||
val dateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")
|
||||
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"))
|
||||
server.enqueue(MockResponse().setBody("<?xml version=\"1.0\"?><api><query><userinfo id=\"1000\" name=\"testusername\" blockid=\"3000\" blockedby=\"blockerusername\" blockedbyid=\"1001\" blockreason=\"testing\" blockedtimestamp=\"2018-05-24T15:32:09Z\" blockexpiry=\"" + dateFormat.format(expiredDate) + "\"></userinfo></query></api>"))
|
||||
|
||||
val result = testObject.isUserBlocked();
|
||||
|
||||
assertBasicRequestParameters(server, "GET").let { userBlockedRequest ->
|
||||
parseQueryParams(userBlockedRequest).let { body ->
|
||||
assertEquals("xml", body["format"])
|
||||
assertEquals("query", body["action"])
|
||||
assertEquals("userinfo", body["meta"])
|
||||
assertEquals("blockinfo", body["uiprop"])
|
||||
}
|
||||
}
|
||||
|
||||
assertFalse(result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun isUserBlockedForNotBlockedUser() {
|
||||
server.enqueue(MockResponse().setBody("<?xml version=\"1.0\"?><api><query><userinfo id=\"1000\" name=\"testusername\"></userinfo></query></api>"))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue