Changeset View
Changeset View
Standalone View
Standalone View
src/test/java/net/wildfyre/http/RequestTest.kt
Show First 20 Lines • Show All 81 Lines • ▼ Show 20 Lines | fun createTmpUser(): LoggedUser { | ||||
val username = "tmp${rnd.nextInt()}" | val username = "tmp${rnd.nextInt()}" | ||||
Request(POST, "/account/register/") | Request(POST, "/account/register/") | ||||
.addJson(JsonObject() | .addJson(JsonObject() | ||||
.add("username", username) | .add("username", username) | ||||
.add("email", "123456@wildfyre.net") | .add("email", "123456@wildfyre.net") | ||||
.add("password", password) | .add("password", password) | ||||
.add("captcha", "123456")) // the field cannot be blank, but is useless in test mode | .add("captcha", "123456")) // the field cannot be blank, but is useless in test mode | ||||
.getJson().asObject() | .getJson().asObject() | ||||
CLOVIS: @Info-Screen This is the request that breaks because of wrong captcha | |||||
WildFyre.disconnect() | WildFyre.disconnect() | ||||
return WildFyre.connect(username, password) | return WildFyre.connect(username, password) | ||||
} | } | ||||
@Test( timeout = 5000L ) | @Test | ||||
fun testCreateTmpUser() { | |||||
val me = createTmpUser() | |||||
assertTrue(me.name.startsWith("tmp")) | |||||
} | |||||
@Test | |||||
fun testPatchMethod() { | fun testPatchMethod() { | ||||
val me = createTmpUser() | val me = createTmpUser() | ||||
val test = "This is a test" | val test = "This is a test" | ||||
me.setBio(test) | me.bio = test | ||||
assertEquals(test, me.bio()) | assertEquals(test, me.bio) | ||||
} | } | ||||
@Test( timeout = 5000L ) // timeout of 5 seconds | @Test | ||||
fun testMultipart() { | fun testMultipart() { | ||||
createTmpUser() | createTmpUser() | ||||
val f = resources["wf.png"] | val f = resources["wf.png"] | ||||
assertTrue("The file ${f.absolutePath} should exist!", f.exists()) | assertTrue("The file ${f.absolutePath} should exist!", f.exists()) | ||||
val json = Request(Method.PATCH, "/users/") | val json = Request(Method.PATCH, "/users/") | ||||
.addFile("avatar", f) | .addFile("avatar", f) | ||||
.addJson(JsonObject().add("bio", "test")) | .addJson(JsonObject().add("bio", "test")) | ||||
.addToken() | .addToken() | ||||
.getJson() | .getJson() | ||||
println(json.toString(PRETTY_PRINT)) | println(json.toString(PRETTY_PRINT)) //TODO there's no test here! | ||||
} | } | ||||
companion object { | companion object { | ||||
// The inspector *thinks* it's possible, but it's not, | val token: String by lazy { | ||||
// because it needs to be done when the class is loaded | Request(POST, "/account/auth/") | ||||
// (init) and NOT when the variables are instantiated. | |||||
@Suppress("JoinDeclarationAndAssignment") | |||||
val token: String | |||||
val resources = File("src/test/resources") | |||||
init { | |||||
token = Request(POST, "/account/auth/") | |||||
.addJson(JsonObject() | .addJson(JsonObject() | ||||
.add("username", "user") | .add("username", "user") | ||||
.add("password", "password$123")) | .add("password", "password$123")) | ||||
.getJson().asObject() | .getJson().asObject() | ||||
.getString("token", null) | .getString("token", null) | ||||
} | } | ||||
@JvmField val resources = File("src/test/resources") | |||||
/** | /** | ||||
* Connects to the test database. If the database is not running on this device, this will obviously fail. | * Connects to the test database. If the database is not running on this device, this will obviously fail. | ||||
* @return The logged user. | * @return The logged user. | ||||
*/ | */ | ||||
fun connectToTestDB(): LoggedUser { | fun connectToTestDB(): LoggedUser { | ||||
return WildFyre.connect(token) | return WildFyre.connect(token) | ||||
} | } | ||||
/** | /** | ||||
* Gets a file in the current directory. | * Gets a file in the current directory. | ||||
*/ | */ | ||||
internal operator fun File.get(name: String) = File(this, name) | internal operator fun File.get(name: String) = File(this, name) | ||||
} | } | ||||
} | } |
@Info-Screen This is the request that breaks because of wrong captcha