From f0bce715063eeb5cfa14e858c1a09bf1dce0e9f3 Mon Sep 17 00:00:00 2001
From: Torsten Grote <t@grobox.de>
Date: Tue, 19 Oct 2021 13:18:00 -0300
Subject: [PATCH] Add more helper methods to IntegrationTest base class

---
 .../mailbox/core/server/IntegrationTest.kt    | 39 +++++++++++++++++++
 1 file changed, 39 insertions(+)

diff --git a/mailbox-core/src/test/java/org/briarproject/mailbox/core/server/IntegrationTest.kt b/mailbox-core/src/test/java/org/briarproject/mailbox/core/server/IntegrationTest.kt
index 0938a91c..1b2e3399 100644
--- a/mailbox-core/src/test/java/org/briarproject/mailbox/core/server/IntegrationTest.kt
+++ b/mailbox-core/src/test/java/org/briarproject/mailbox/core/server/IntegrationTest.kt
@@ -4,10 +4,17 @@ import io.ktor.client.HttpClient
 import io.ktor.client.engine.cio.CIO
 import io.ktor.client.features.json.JacksonSerializer
 import io.ktor.client.features.json.JsonFeature
+import io.ktor.client.request.HttpRequestBuilder
+import io.ktor.client.request.headers
+import io.ktor.http.HttpHeaders
 import org.briarproject.mailbox.core.DaggerTestComponent
 import org.briarproject.mailbox.core.TestComponent
 import org.briarproject.mailbox.core.TestModule
+import org.briarproject.mailbox.core.TestUtils.getNewRandomContact
+import org.briarproject.mailbox.core.TestUtils.getNewRandomId
+import org.briarproject.mailbox.core.contacts.Contact
 import org.briarproject.mailbox.core.server.WebServerManager.Companion.PORT
+import org.briarproject.mailbox.core.settings.Settings
 import org.junit.jupiter.api.AfterAll
 import org.junit.jupiter.api.BeforeAll
 import org.junit.jupiter.api.TestInstance
@@ -28,12 +35,23 @@ abstract class IntegrationTest {
     }
     protected val baseUrl = "http://127.0.0.1:$PORT"
 
+    protected val ownerToken = getNewRandomId()
+    protected val token = getNewRandomId()
+    protected val id = getNewRandomId()
+    protected val contact1 = getNewRandomContact()
+    protected val contact2 = getNewRandomContact()
+
     @BeforeAll
     fun setUp(@TempDir tempDir: File) {
         testComponent = DaggerTestComponent.builder().testModule(TestModule(tempDir)).build()
         testComponent.injectCoreEagerSingletons()
         lifecycleManager.startServices()
         lifecycleManager.waitForStartup()
+        initDb()
+    }
+
+    open fun initDb() {
+        // sub-classes can initialize the DB here as needed
     }
 
     @AfterAll
@@ -42,4 +60,25 @@ abstract class IntegrationTest {
         lifecycleManager.waitForShutdown()
     }
 
+    protected fun addOwnerToken() {
+        val settingsManager = testComponent.getSettingsManager()
+        val settings = Settings()
+        settings[SETTINGS_OWNER_TOKEN] = ownerToken
+        settingsManager.mergeSettings(settings, SETTINGS_NAMESPACE_OWNER)
+    }
+
+    protected fun addContact(c: Contact) {
+        val db = testComponent.getDatabase()
+        db.transaction(false) { txn ->
+            db.addContact(txn, c)
+        }
+    }
+
+    protected fun HttpRequestBuilder.authenticateWithToken(t: String) {
+        headers {
+            @Suppress("EXPERIMENTAL_API_USAGE_FUTURE_ERROR")
+            append(HttpHeaders.Authorization, "Bearer $t")
+        }
+    }
+
 }
-- 
GitLab