Session test
This commit is contained in:
parent
beb4e004bf
commit
d7a28bf713
3 changed files with 47 additions and 5 deletions
|
@ -4,9 +4,9 @@ import info.nightscout.androidaps.plugins.general.tidepool.messages.AuthReplyMes
|
||||||
import info.nightscout.androidaps.plugins.general.tidepool.messages.DatasetReplyMessage
|
import info.nightscout.androidaps.plugins.general.tidepool.messages.DatasetReplyMessage
|
||||||
import okhttp3.Headers
|
import okhttp3.Headers
|
||||||
|
|
||||||
class Session(var authHeader: String?, private var sessionTokenHeader: String) {
|
class Session(val authHeader: String?,
|
||||||
|
private val sessionTokenHeader: String,
|
||||||
val service = TidepoolUploader.getRetrofitInstance()?.create(TidepoolApiService::class.java)
|
val service: TidepoolApiService?) {
|
||||||
|
|
||||||
internal var token: String? = null
|
internal var token: String? = null
|
||||||
internal var authReply: AuthReplyMessage? = null
|
internal var authReply: AuthReplyMessage? = null
|
||||||
|
|
|
@ -63,6 +63,11 @@ object TidepoolUploader {
|
||||||
return retrofit
|
return retrofit
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun createSession(): Session {
|
||||||
|
val service = getRetrofitInstance()?.create(TidepoolApiService::class.java)
|
||||||
|
return Session(AuthRequestMessage.getAuthRequestHeader(), SESSION_TOKEN_HEADER, service)
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: call on preference change
|
// TODO: call on preference change
|
||||||
fun resetInstance() {
|
fun resetInstance() {
|
||||||
retrofit = null
|
retrofit = null
|
||||||
|
@ -80,7 +85,7 @@ object TidepoolUploader {
|
||||||
}
|
}
|
||||||
// TODO failure backoff
|
// TODO failure backoff
|
||||||
extendWakeLock(30000)
|
extendWakeLock(30000)
|
||||||
session = Session(AuthRequestMessage.getAuthRequestHeader(), SESSION_TOKEN_HEADER)
|
session = createSession()
|
||||||
val authHeader = session?.authHeader
|
val authHeader = session?.authHeader
|
||||||
if (authHeader != null) {
|
if (authHeader != null) {
|
||||||
connectionStatus = TidepoolUploader.ConnectionStatus.CONNECTING
|
connectionStatus = TidepoolUploader.ConnectionStatus.CONNECTING
|
||||||
|
@ -104,7 +109,7 @@ object TidepoolUploader {
|
||||||
}
|
}
|
||||||
|
|
||||||
fun testLogin(rootContext: Context) {
|
fun testLogin(rootContext: Context) {
|
||||||
val session = Session(AuthRequestMessage.getAuthRequestHeader(), SESSION_TOKEN_HEADER)
|
val session = createSession()
|
||||||
session.authHeader?.let {
|
session.authHeader?.let {
|
||||||
val call = session.service?.getLogin(it)
|
val call = session.service?.getLogin(it)
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,37 @@
|
||||||
|
package info.nightscout.androidaps.plugins.general.tidepool.comm
|
||||||
|
|
||||||
|
import info.nightscout.androidaps.plugins.general.tidepool.messages.AuthReplyMessage
|
||||||
|
import info.nightscout.androidaps.plugins.general.tidepool.messages.DatasetReplyMessage
|
||||||
|
import org.junit.Assert.assertEquals
|
||||||
|
import org.junit.Assert.assertNull
|
||||||
|
import org.junit.Test
|
||||||
|
import org.junit.runner.RunWith
|
||||||
|
import org.powermock.modules.junit4.PowerMockRunner
|
||||||
|
|
||||||
|
@RunWith(PowerMockRunner::class)
|
||||||
|
class SessionTest {
|
||||||
|
@Test
|
||||||
|
fun populateBody() {
|
||||||
|
val session = Session("", "", null)
|
||||||
|
assertNull(session.authReply)
|
||||||
|
|
||||||
|
// test authReply
|
||||||
|
val authReplyMessage = AuthReplyMessage()
|
||||||
|
session.populateBody(authReplyMessage)
|
||||||
|
assertEquals(authReplyMessage, session.authReply)
|
||||||
|
|
||||||
|
// test datasetReply
|
||||||
|
val datasetReplyMessage = DatasetReplyMessage()
|
||||||
|
assertNull(session.datasetReply)
|
||||||
|
session.populateBody(datasetReplyMessage)
|
||||||
|
assertEquals(datasetReplyMessage, session.datasetReply)
|
||||||
|
|
||||||
|
// test datasetReply as array
|
||||||
|
val list: List<DatasetReplyMessage> = listOf(datasetReplyMessage)
|
||||||
|
session.datasetReply = null
|
||||||
|
assertNull(session.datasetReply)
|
||||||
|
session.populateBody(list)
|
||||||
|
assertEquals(datasetReplyMessage, session.datasetReply)
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue