Skip to content
This repository has been archived by the owner on Jan 31, 2024. It is now read-only.

createProfileActivity: mock Authenticator #269

Merged
merged 3 commits into from
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.ext.junit.runners.AndroidJUnit4
import ch.sdp.vibester.R
import ch.sdp.vibester.TestMode
import ch.sdp.vibester.auth.FireBaseAuthenticator
import ch.sdp.vibester.database.DataGetter
import ch.sdp.vibester.user.User
import com.google.firebase.auth.FirebaseUser
import dagger.hilt.android.testing.BindValue
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
Expand All @@ -39,21 +41,36 @@ class CreateProfileActivityTest {
}

@BindValue @JvmField
val mockUsersRepo = mockk<DataGetter>()
val mockAuthenticator = mockk<FireBaseAuthenticator>()

private fun createMockInvocation(email: String) {
every {mockUsersRepo.createUser(any(), any(), any(), any())} answers {
lastArg<(String) -> Unit>().invoke(email)
}
private fun createMockAuthenticator() {
val mockUser = createMockUser()
every { mockAuthenticator.getCurrUser() } returns mockUser
every { mockAuthenticator.getCurrUID() } returns mockUser.uid
}

every { mockUsersRepo.getUserData(any(), any()) } answers {
secondArg<(User) -> Unit>().invoke(User())
}
private fun createMockUser(): FirebaseUser {
val email = "[email protected]"
val uid = "mockuseruid"
val mockUser = mockk<FirebaseUser>()
every { mockUser.email } returns email
every { mockUser.uid } returns uid
return mockUser
}

every { mockUsersRepo.getCurrentUser() } answers {
null
}
@BindValue @JvmField
val mockDataGetter = mockk<DataGetter>()

private fun createMockDataGetter(email: String) {
every {mockDataGetter.createUser(any(), any(), any(), any())} answers {
lastArg<(String) -> Unit>().invoke(email)
}
every { mockDataGetter.getUserData(any(), any()) } answers {secondArg<(User) -> Unit>().invoke(User())}
every { mockDataGetter.getCurrentUser() } answers {null }
every { mockDataGetter.setSubFieldValue(any(), any(), any(), any()) } answers {}
every { mockDataGetter.updateFieldInt(any(), any(), any(), any()) } answers {}
every { mockDataGetter.setFieldValue(any(), any(), any()) } answers {}
every { mockDataGetter.updateSubFieldInt(any(), any(), any(), any(), any()) } answers {}
}

@After
Expand All @@ -69,9 +86,9 @@ class CreateProfileActivityTest {

val intent = Intent(ApplicationProvider.getApplicationContext(), CreateProfileActivity::class.java)
intent.putExtra("email", mockEmail)
intent.putExtra("isUnitTest", true)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nooooo bye my friend isUnitTest, you had a great life 😂😂😂😂 !!!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahahahahahhahaa bye-bye and never come back please


createMockInvocation(mockEmail)
createMockDataGetter(mockEmail)
createMockAuthenticator()

val scn: ActivityScenario<CreateProfileActivity> = ActivityScenario.launch(intent)

Expand All @@ -83,7 +100,6 @@ class CreateProfileActivityTest {

Intents.intended(IntentMatchers.hasComponent(ProfileActivity::class.java.name))
Intents.intended(IntentMatchers.hasExtra("email", mockEmail))
Intents.intended(IntentMatchers.hasExtra("isUnitTest", true))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import javax.inject.Inject
@AndroidEntryPoint
class CreateProfileActivity : AppCompatActivity() {

@Inject
lateinit var authenticator: FireBaseAuthenticator

@Inject
lateinit var dataGetter: DataGetter

Expand All @@ -26,24 +29,18 @@ class CreateProfileActivity : AppCompatActivity() {

private val REQUEST_CODE = 500

var isUnitTest: Boolean = false

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_create_profile)

isUnitTest = intent.getBooleanExtra("isUnitTest", false)

val email = intent.getStringExtra("email").toString()
val username = findViewById<EditText>(R.id.accountUsername)

val btCreateAcc = findViewById<Button>(R.id.createButton)
val btnUploadImg = findViewById<Button>(R.id.uploadImg)

btCreateAcc.setOnClickListener {
if (!TestMode.isTest()){
dataGetter.setFieldValue(FireBaseAuthenticator.getCurrentUID(), "username", username.text.toString())
}
dataGetter.setFieldValue(authenticator.getCurrUID(), "username", username.text.toString())
startNewActivity(email)
}

Expand All @@ -55,7 +52,6 @@ class CreateProfileActivity : AppCompatActivity() {
private fun startNewActivity(email: String) {
val newIntent = Intent(this, ProfileActivity::class.java)
newIntent.putExtra("email", email)
newIntent.putExtra("isUnitTest", isUnitTest)

startActivity(newIntent)
}
Expand All @@ -76,7 +72,7 @@ class CreateProfileActivity : AppCompatActivity() {
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_CODE){
imageGetter.uploadFile("profileImg/${FireBaseAuthenticator.getCurrentUID()}", data?.data!!) { updateUI() }
imageGetter.uploadFile("profileImg/${authenticator.getCurrUID()}", data?.data!!) { updateUI() }
}
}
}
11 changes: 11 additions & 0 deletions app/src/main/java/ch/sdp/vibester/auth/FireBaseAuthenticator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ class FireBaseAuthenticator @Inject constructor() {
return Firebase.auth.currentUser
}

/**
* Getter for the current user ID
*/
fun getCurrUID(): String {
var uid = ""
if (isLoggedIn()) {
uid = FirebaseAuth.getInstance().currentUser!!.uid
}
return uid
}
Comment on lines +87 to +96
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/**
* Getter for the current user ID
*/
fun getCurrUID(): String {
var uid = ""
if (isLoggedIn()) {
uid = FirebaseAuth.getInstance().currentUser!!.uid
}
return uid
}

See 40 lines above !!!
getCurrentUID() exactly the same 😂

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, but when you mock, you cant access one from the above

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will mock others as well, and remove those from companion object. sounds good?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohh okay I see, yes maybe removing the companion object may be a good idea because I find it weird otherwise to have two times the same code.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is... I decided to remove one by one, so it easier to check!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will remove these function on the next pr since there a lot of dependencies. My next pr (where these repetetive functions are removed) is almost done!



/**
* A function to log in with email and password
Expand Down