Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Commit

Permalink
test(access): create test for storage
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreaBrighi committed Apr 28, 2023
1 parent 9b47020 commit c072a8e
Showing 1 changed file with 102 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package com.intelligentbackpack.accessdata

import android.content.Context
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.intelligentbackpack.accessdata.storage.StorageUser
import com.intelligentbackpack.accessdata.storage.StorageUserImpl
import com.intelligentbackpack.accessdomain.entities.User
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Assert.assertEquals
import org.junit.Assert.assertThrows
import org.junit.Test
import org.junit.runner.RunWith

/**
* Instrumented test, which will execute on an Android device.
* Tests for [StorageUser]
*/
@RunWith(AndroidJUnit4::class)
class StorageUserInstrumentedTest {

@Test
fun initiallyAUserIsntLogged() {
val appContext =
InstrumentationRegistry
.getInstrumentation()
.targetContext
val storage: StorageUser = StorageUserImpl(appContext)
assertFalse(storage.isUserSaved())
}

@Test
fun saveUser() {
val appContext =
InstrumentationRegistry
.getInstrumentation()
.targetContext
val storage: StorageUser = StorageUserImpl(appContext)
val expectedUser = User.build {
email = "test@gmail.com"
password = "Test#1234"
name = "test"
surname = "test"
}
storage.saveUser(expectedUser)
assertTrue(storage.isUserSaved())
val savedUser = storage.getUser()
assertEquals(expectedUser, savedUser)
}

@Test
fun noUserSaved() {
val appContext =
InstrumentationRegistry
.getInstrumentation()
.targetContext
val storage: StorageUser = StorageUserImpl(appContext)
assertFalse(storage.isUserSaved())
assertThrows(IllegalStateException::class.java) {
storage.getUser()
}
}

@Test
fun deleteUser() {
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
val storage: StorageUser = StorageUserImpl(appContext)
val expectedUser = User.build {
email = "test@gmail.com"
password = "Test#1234"
name = "test"
surname = "test"
}
storage.saveUser(expectedUser)
assertTrue(storage.isUserSaved())
storage.deleteUser()
assertFalse(storage.isUserSaved())
}

@Test
fun passwordIsEncrypted() {
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
val storage: StorageUser = StorageUserImpl(appContext)
val expectedUser = User.build {
email = "test@gmail.com"
password = "Test#1234"
name = "test"
surname = "test"
}
storage.saveUser(expectedUser)
assertTrue(storage.isUserSaved())
assertFalse(
isPresentElementsFromSharedPreferences(appContext, "password")
)
}

private fun isPresentElementsFromSharedPreferences(context: Context, field: String): Boolean {
val sharedPref = context.getSharedPreferences("IntelligentBackpackSharedPref", Context.MODE_PRIVATE)
return sharedPref.contains(field)
}
}

0 comments on commit c072a8e

Please sign in to comment.