Skip to content

Commit

Permalink
[MWCore] Bug fixes and updates (#2688)
Browse files Browse the repository at this point in the history
* fixes

* Update AccountAuthenticatorTest.kt

* Update AccountAuthenticatorTest.kt

* Update AccountAuthenticatorTest.kt

* Sort patients by Organization ID

* Update AccountAuthenticatorTest.kt

* Update build.gradle

* fix hiv dao tests

---------

Co-authored-by: Comfort Mwalija <calmwalija@gmail.com>
  • Loading branch information
sevenreup and calmwalija authored Aug 24, 2023
1 parent e49e2bf commit f54ee3e
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,18 @@ constructor(
}
}

fun logoutLocal(): Boolean {
val account = tokenAuthenticator.findAccount()
if (account != null) {
accountManager.invalidateAuthToken(
account.type,
accountManager.peekAuthToken(account, AUTH_TOKEN_TYPE)
)
return true
}
return false
}

fun validateLoginCredentials(username: String, password: CharArray) =
tokenAuthenticator.validateSavedLoginCredentials(username, password)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import org.smartregister.fhircore.engine.domain.model.RegisterData
import org.smartregister.fhircore.engine.domain.repository.PatientDao
import org.smartregister.fhircore.engine.domain.repository.RegisterDao
import org.smartregister.fhircore.engine.domain.util.PaginationConstant
import org.smartregister.fhircore.engine.util.SharedPreferencesHelper
import org.smartregister.fhircore.engine.util.extension.activelyBreastfeeding
import org.smartregister.fhircore.engine.util.extension.canonical
import org.smartregister.fhircore.engine.util.extension.canonicalName
Expand Down Expand Up @@ -78,13 +79,19 @@ constructor(
val configurationRegistry: ConfigurationRegistry
) : RegisterDao, PatientDao {

private val code = defaultRepository.sharedPreferencesHelper.organisationCode()

fun isValidPatient(patient: Patient): Boolean =
patient.active &&
!patient.hasDeceased() &&
patient.hasName() &&
patient.hasGender() &&
patient.meta.tag.none { it.code.equals(HAPI_MDM_TAG, true) }
patient.meta.tag.none { it.code.equals(HAPI_MDM_TAG, true) } &&
patient.belongsTo(code)

init {
Timber.e(code)
}
fun hivPatientIdentifier(patient: Patient): String =
// would either be an ART or HCC number
patient.extractOfficialIdentifier() ?: ResourceValue.BLANK
Expand Down Expand Up @@ -418,6 +425,8 @@ constructor(
companion object {
const val HAPI_MDM_TAG = "HAPI-MDM"
const val LINKED_CHILD_AGE_LIMIT = 20
const val ORGANISATION_SYSTEM = "http://smartregister.org/fhir/organization-tag"
const val ORGANISATION_DISPLAY = "Practitioner Organization"
}
}

Expand All @@ -433,3 +442,13 @@ suspend fun DefaultRepository.isPatientPregnant(patient: Patient) =

suspend fun DefaultRepository.isPatientBreastfeeding(patient: Patient) =
patientConditions(patient.logicalId).activelyBreastfeeding()

fun SharedPreferencesHelper.organisationCode() =
read(ResourceType.Organization.name, null)?.filter { it.isDigit() } ?: ""

infix fun Patient.belongsTo(code: String) =
meta.tag.any {
it.code == code &&
it.system == HivRegisterDao.ORGANISATION_SYSTEM &&
it.display == HivRegisterDao.ORGANISATION_DISPLAY
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import java.net.UnknownHostException
import javax.inject.Inject
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
Expand Down Expand Up @@ -216,7 +217,17 @@ constructor(
.onSuccess { fetchPractitioner(onFetchUserInfo, onFetchPractitioner) }
.onFailure {
_showProgressBar.postValue(false)
_loginErrorState.postValue(LoginErrorState.UNKNOWN_HOST)
var errorState = LoginErrorState.ERROR_FETCHING_USER

if (it is HttpException) {
when (it.code()) {
401 -> errorState = LoginErrorState.INVALID_CREDENTIALS
}
} else if (it is UnknownHostException) {
errorState = LoginErrorState.UNKNOWN_HOST
}

_loginErrorState.postValue(errorState)
Timber.e(it)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import org.hl7.fhir.r4.model.Patient
import org.hl7.fhir.r4.model.ResourceType
import org.hl7.fhir.r4.model.StringType
import org.smartregister.fhircore.engine.configuration.ConfigurationRegistry
import org.smartregister.fhircore.engine.data.local.register.dao.HivRegisterDao.Companion.ORGANISATION_DISPLAY
import org.smartregister.fhircore.engine.data.local.register.dao.HivRegisterDao.Companion.ORGANISATION_SYSTEM
import org.smartregister.fhircore.engine.data.remote.fhir.resource.FhirResourceDataSource
import org.smartregister.fhircore.engine.data.remote.fhir.resource.FhirResourceService
import org.smartregister.fhircore.engine.robolectric.RobolectricTest.Companion.readFile
Expand Down Expand Up @@ -153,6 +155,14 @@ object Faker {
}
)

this.meta.addTag(
Coding().apply {
system = ORGANISATION_SYSTEM
code = "123"
display = ORGANISATION_DISPLAY
}
)

this.generalPractitionerFirstRep.apply { reference = practitionerReference }
this.deceased = deceasedBooleanType
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,16 @@ import java.net.UnknownHostException
import java.util.Locale
import java.util.concurrent.TimeUnit
import javax.inject.Inject
import kotlin.test.assertFalse
import kotlin.test.assertTrue
import kotlinx.coroutines.ExperimentalCoroutinesApi
import org.junit.Assert
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.smartregister.fhircore.engine.configuration.app.ConfigService
import org.smartregister.fhircore.engine.data.remote.shared.TokenAuthenticator
import org.smartregister.fhircore.engine.data.remote.shared.TokenAuthenticator.Companion.AUTH_TOKEN_TYPE
import org.smartregister.fhircore.engine.robolectric.RobolectricTest
import org.smartregister.fhircore.engine.util.DispatcherProvider
import org.smartregister.fhircore.engine.util.SecureSharedPreference
Expand Down Expand Up @@ -362,6 +365,25 @@ class AccountAuthenticatorTest : RobolectricTest() {
verify { tokenAuthenticator.logout() }
}

@Test
fun testThatLogoutLocalWithAccount() {
val account = Account("testAccountName", "testAccountType")
every { tokenAuthenticator.findAccount() } returns account
every { accountManager.invalidateAuthToken(any(), any()) } just runs
every { accountManager.peekAuthToken(any(), any()) } returns ""
accountAuthenticator.logoutLocal()
verify { accountManager.invalidateAuthToken(any(), any()) }
}

@Test
fun testThatLogoutLocalNoAccount() {
every { tokenAuthenticator.findAccount() } returns null
every { accountManager.invalidateAuthToken(any(), any()) } just runs
every { accountManager.peekAuthToken(any(), any()) } returns ""
val value = accountAuthenticator.logoutLocal()
assertFalse(value)
}

@Test
fun testValidateLoginCredentials() {
every { tokenAuthenticator.validateSavedLoginCredentials(any(), any()) } returns true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ internal class HivRegisterDaoTest : RobolectricTest() {
coEvery { fhirEngine.get(ResourceType.Task, testTask1.logicalId) } returns testTask1
coEvery { fhirEngine.get(ResourceType.Task, testTask2.logicalId) } returns testTask2

every { sharedPreferencesHelper.organisationCode() } returns "123"

coEvery { fhirEngine.update(any()) } just runs

coEvery { fhirEngine.search<Resource>(any<Search>()) } answers
Expand Down
12 changes: 6 additions & 6 deletions android/quest/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -173,22 +173,22 @@ android {
dimension "apps"
applicationIdSuffix ".mwcore"
versionNameSuffix "-mwcore"
versionCode 20
versionName "0.1.10"
versionCode 22
versionName "0.1.12"
}
mwcoreDev {
dimension "apps"
applicationIdSuffix ".mwcoreDev"
versionNameSuffix "-mwcoreDev"
versionCode 20
versionName "0.1.10"
versionCode 21
versionName "0.1.12"
}
mwcoreProd {
dimension "apps"
applicationIdSuffix ".mwcoreProd"
versionNameSuffix "-mwcoreProd"
versionCode 1
versionName "0.0.1"
versionCode 2
versionName "0.0.2"
}
afyayangu {
dimension "apps"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import android.app.Activity
import android.os.Handler
import android.os.Looper
import androidx.ui.unit.inMilliseconds
import androidx.ui.unit.minutes
import androidx.ui.unit.seconds
import timber.log.Timber

class AppInActivityListener(val ignoreList: List<String>, onTimeLapse: () -> Unit) {
Expand All @@ -36,7 +36,7 @@ class AppInActivityListener(val ignoreList: List<String>, onTimeLapse: () -> Uni

fun start() {
Timber.i("App is in background")
handler.postDelayed(runnable, 10.minutes.inMilliseconds())
handler.postDelayed(runnable, 1.seconds.inMilliseconds())
}

fun stop() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ import org.smartregister.fhircore.engine.data.remote.fhir.resource.ReferenceUrlR
import org.smartregister.fhircore.engine.ui.appsetting.AppSettingActivity
import org.smartregister.fhircore.engine.ui.login.LoginActivity
import org.smartregister.fhircore.engine.ui.questionnaire.QuestionnaireItemViewHolderFactoryMatchersProviderFactoryImpl
import org.smartregister.fhircore.engine.util.extension.getActivity
import org.smartregister.fhircore.engine.util.extension.launchActivityWithNoBackStackHistory
import org.smartregister.fhircore.engine.util.extension.showToast
import timber.log.Timber

Expand Down Expand Up @@ -139,21 +141,16 @@ class QuestApplication :

override fun onStart(owner: LifecycleOwner) {
appInActivityListener.stop()
if (mForegroundActivityContext != null) {
accountAuthenticator.loadActiveAccount(
onValidTokenMissing = {
if (it.component!!.className != mForegroundActivityContext!!::class.java.name) {
mForegroundActivityContext!!.startActivity(it)
}
}
)
}
mForegroundActivityContext
?.takeIf {
val name = it::class.java.name
name !in activitiesAccessWithoutAuth
}
?.let { accountAuthenticator.confirmActiveAccount { intent -> it.startActivity(intent) } }
?.let {
mForegroundActivityContext
?.getActivity()
?.launchActivityWithNoBackStackHistory<LoginActivity>()
}
}

private fun initANRWatcher() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import org.smartregister.fhircore.engine.configuration.app.AppConfigClassificati
import org.smartregister.fhircore.engine.configuration.app.ApplicationConfiguration
import org.smartregister.fhircore.engine.configuration.app.ConfigService
import org.smartregister.fhircore.engine.sync.SyncBroadcaster
import org.smartregister.fhircore.engine.ui.appsetting.AppSettingActivity
import org.smartregister.fhircore.engine.ui.login.LoginActivity
import org.smartregister.fhircore.engine.util.SecureSharedPreference
import org.smartregister.fhircore.engine.util.SharedPreferenceKey
Expand Down Expand Up @@ -191,9 +190,7 @@ constructor(
}

fun onTimeOut(context: Context) {
accountAuthenticator.invalidateSession {
context.getActivity()?.launchActivityWithNoBackStackHistory<AppSettingActivity>()
}
accountAuthenticator.logoutLocal()
}

fun onTaskComplete(id: String?) {
Expand Down

0 comments on commit f54ee3e

Please sign in to comment.