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 okhttp3.Headers
|
||||
|
||||
class Session(var authHeader: String?, private var sessionTokenHeader: String) {
|
||||
|
||||
val service = TidepoolUploader.getRetrofitInstance()?.create(TidepoolApiService::class.java)
|
||||
class Session(val authHeader: String?,
|
||||
private val sessionTokenHeader: String,
|
||||
val service: TidepoolApiService?) {
|
||||
|
||||
internal var token: String? = null
|
||||
internal var authReply: AuthReplyMessage? = null
|
||||
|
|
|
@ -63,6 +63,11 @@ object TidepoolUploader {
|
|||
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
|
||||
fun resetInstance() {
|
||||
retrofit = null
|
||||
|
@ -80,7 +85,7 @@ object TidepoolUploader {
|
|||
}
|
||||
// TODO failure backoff
|
||||
extendWakeLock(30000)
|
||||
session = Session(AuthRequestMessage.getAuthRequestHeader(), SESSION_TOKEN_HEADER)
|
||||
session = createSession()
|
||||
val authHeader = session?.authHeader
|
||||
if (authHeader != null) {
|
||||
connectionStatus = TidepoolUploader.ConnectionStatus.CONNECTING
|
||||
|
@ -104,7 +109,7 @@ object TidepoolUploader {
|
|||
}
|
||||
|
||||
fun testLogin(rootContext: Context) {
|
||||
val session = Session(AuthRequestMessage.getAuthRequestHeader(), SESSION_TOKEN_HEADER)
|
||||
val session = createSession()
|
||||
session.authHeader?.let {
|
||||
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