Skip to content

Commit

Permalink
Merge pull request #346 from G12-Wanderwave/feature/link-profile-to-a…
Browse files Browse the repository at this point in the history
…uth/main

Feature/link profile to auth/main
  • Loading branch information
yzueger authored May 19, 2024
2 parents e95a829 + 1df9b2a commit f5a834c
Show file tree
Hide file tree
Showing 16 changed files with 158 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -469,9 +469,28 @@ public class BeaconConnectionTest {
hashMapOf(
"id" to getTestBeacon.id,
"location" to getTestBeacon.location.toMap(),
"tracks" to getTestBeacon.profileAndTrack.map { it.toMap() })
"tracks" to
getTestBeacon.profileAndTrack.map {
hashMapOf(
"creator" to firestore.collection("users").document(profile.firebaseUid),
"track" to firestore.collection("tracks").document(track.id))
})

every { mockTransaction.get(any<DocumentReference>()) } answers
{
val reference = arg<DocumentReference>(0)

when {
reference.path.contains("beacons") -> mockDocumentSnapshot
reference.path.contains("users") ->
mockk<DocumentSnapshot>() { every { getData() } returns profile.toMap() }
reference.path.contains("tracks") ->
mockk<DocumentSnapshot>() { every { getData() } returns track.toMap() }
reference.path.equals("") -> mockDocumentSnapshot
else -> throw IllegalStateException("Invalid reference path: ${reference.path}")
}
}

every { mockTransaction.get(any<DocumentReference>()) } returns mockDocumentSnapshot
every { mockTransaction.update(any<DocumentReference>(), any<String>(), any()) } answers
{
mockTransaction
Expand All @@ -481,7 +500,26 @@ public class BeaconConnectionTest {
every { mockDocumentSnapshot.id } returns getTestBeacon.id
every { mockDocumentSnapshot.get("location") } returns getTestBeacon.location.toMap()
every { mockDocumentSnapshot.get("tracks") } returns
getTestBeacon.profileAndTrack.map { it.toMap() }
getTestBeacon.profileAndTrack.map {
hashMapOf(
"creator" to firestore.collection("users").document(profile.firebaseUid),
"track" to firestore.collection("tracks").document(track.id))
}

profile.toMap().forEach { (key, value) ->
every { mockDocumentSnapshot.get(key) } returns value
(value as? String)?.let { every { mockDocumentSnapshot.getString(key) } returns it }
(value as? Int)?.let { every { mockDocumentSnapshot.getLong(key) } returns it.toLong() }
(value as? Boolean)?.let { every { mockDocumentSnapshot.getBoolean(key) } returns it }
}
track.toMap().forEach { (key, value) ->
every { mockDocumentSnapshot.get(key) } returns value
(value as? String)?.let { every { mockDocumentSnapshot.getString(key) } returns it }
(value as? Int)?.let { every { mockDocumentSnapshot.getLong(key) } returns it.toLong() }
(value as? Boolean)?.let { every { mockDocumentSnapshot.getBoolean(key) } returns it }
}

every { mockDocumentSnapshot.getLong(any()) } returns 0

// Define behavior for the addOnSuccessListener method
every { mockTask.addOnSuccessListener(any<OnSuccessListener<Transaction>>()) } answers
Expand All @@ -503,7 +541,7 @@ public class BeaconConnectionTest {
}

// Call the function under test
beaconConnection.addTrackToBeacon(beacon.id, track, {})
beaconConnection.addTrackToBeacon(beacon.id, track, "testing-uid", {})

verify { firestore.runTransaction<Transaction>(any()) }
verify { mockTransaction.get(any<DocumentReference>()) }
Expand Down Expand Up @@ -573,7 +611,7 @@ public class BeaconConnectionTest {

// Call the function under test
try {
beaconConnection.addTrackToBeacon(beacon.id, track, {})
beaconConnection.addTrackToBeacon(beacon.id, track, "testing-uid", {})
fail("Should have thrown an exception")
} catch (e: Exception) {
// Verify that the exception is thrown
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ import ch.epfl.cs311.wanderwave.model.data.Profile
import ch.epfl.cs311.wanderwave.model.data.Track
import ch.epfl.cs311.wanderwave.model.remote.ProfileConnection
import ch.epfl.cs311.wanderwave.model.remote.TrackConnection
import com.google.android.gms.tasks.OnSuccessListener
import com.google.android.gms.tasks.Task
import com.google.firebase.firestore.CollectionReference
import com.google.firebase.firestore.DocumentReference
import com.google.firebase.firestore.DocumentSnapshot
import com.google.firebase.firestore.EventListener
import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.firestore.Query
import com.google.firebase.firestore.QuerySnapshot
import io.mockk.MockKAnnotations
import io.mockk.coVerify
Expand Down Expand Up @@ -297,65 +295,4 @@ public class ProfileConnectionTest {
assert(result.isFailure)
}
}

@Test
fun testIsUidExisting() {
runBlocking {
withTimeout(3000) {
// Pass the mock Firestore instance to your BeaconConnection

val collectionReference = mockk<CollectionReference>()
val query = mockk<Query>()

val mockTask = mockk<Task<QuerySnapshot>>()
val mockQuerySnapshot = mockk<QuerySnapshot>()
val mockDocumentSnapshot =
mockk<DocumentSnapshot>() {
every { exists() } returns true
every { getString("spotifyUid") } returns "testUid"
every { getString("firebaseUid") } returns "testFirebaseUid"
every { getString("firstName") } returns "testFirstName"
every { getString("lastName") } returns "testLastName"
every { getString("description") } returns "testDescription"
every { getLong("numberOfLikes") } returns 0
every { getBoolean("isPublic") } returns false
every { getString("profilePictureUri") } returns ""
}

// Define behavior for the addOnSuccessListener method
every { mockTask.addOnSuccessListener(any<OnSuccessListener<QuerySnapshot>>()) } answers
{
val listener = arg<OnSuccessListener<QuerySnapshot>>(0)
// Define the behavior of the mock DocumentSnapshot here
listener.onSuccess(mockQuerySnapshot)
mockTask
}
every { mockTask.addOnFailureListener(any()) } answers { mockTask }

every { firebaseFirestore.collection(profileConnection.collectionName) } returns
collectionReference
every { collectionReference.whereEqualTo("spotifyUid", "testUid") } returns query
every { query.get() } returns mockTask

every { mockQuerySnapshot.isEmpty } returns false
every { mockQuerySnapshot.size() } returns 1
every { mockQuerySnapshot.documents } returns listOf(mockDocumentSnapshot)

val callback = mockk<(Boolean, Profile?) -> Unit>(relaxed = true)

profileConnection.isUidExisting("testUid", callback)

verify { callback.invoke(true, any()) }

// same but document size is 0
every { mockQuerySnapshot.isEmpty } returns true
every { mockQuerySnapshot.size() } returns 0
every { mockQuerySnapshot.documents } returns listOf()

profileConnection.isUidExisting("testUid", callback)

verify { callback.invoke(false, null) }
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package ch.epfl.cs311.wanderwave.ui
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.navigation.NavHostController
import androidx.test.ext.junit.runners.AndroidJUnit4
import ch.epfl.cs311.wanderwave.model.auth.AuthenticationController
import ch.epfl.cs311.wanderwave.model.auth.AuthenticationUserData
import ch.epfl.cs311.wanderwave.model.data.Beacon
import ch.epfl.cs311.wanderwave.model.data.Location
import ch.epfl.cs311.wanderwave.model.data.Profile
Expand Down Expand Up @@ -41,6 +43,7 @@ class BeaconScreenTest {
@RelaxedMockK private lateinit var trackRepository: TrackRepository
@RelaxedMockK private lateinit var mockNavController: NavHostController
@RelaxedMockK private lateinit var mockSpotifyController: SpotifyController
@RelaxedMockK private lateinit var mockAuthenticationController: AuthenticationController

@Before
fun setup() {
Expand Down Expand Up @@ -74,7 +77,12 @@ class BeaconScreenTest {
val connectResult = SpotifyController.ConnectResult.SUCCESS
every { mockSpotifyController.connectRemote() } returns flowOf(connectResult)

val viewModel = BeaconViewModel(trackRepository, beaconConnection, mockSpotifyController)
every { mockAuthenticationController.getUserData() } returns
AuthenticationUserData("test-uid", "test-email", "test-name", "test-photo-url")

val viewModel =
BeaconViewModel(
trackRepository, beaconConnection, mockSpotifyController, mockAuthenticationController)

composeTestRule.setContent { BeaconScreen(beaconId, mockNavigationActions, viewModel) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ch.epfl.cs311.wanderwave.ui
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.lifecycle.viewModelScope
import androidx.test.ext.junit.runners.AndroidJUnit4
import ch.epfl.cs311.wanderwave.model.auth.AuthenticationController
import ch.epfl.cs311.wanderwave.model.data.Profile
import ch.epfl.cs311.wanderwave.model.remote.ProfileConnection
import ch.epfl.cs311.wanderwave.model.spotify.SpotifyController
Expand Down Expand Up @@ -52,10 +53,12 @@ class EditProfileTest : TestCase(kaspressoBuilder = Kaspresso.Builder.withCompos

@RelaxedMockK private lateinit var spotifyController: SpotifyController

@RelaxedMockK private lateinit var authenticationController: AuthenticationController

@Before
fun setup() {
mockDependencies()
viewModel = ProfileViewModel(profileRepository, spotifyController)
viewModel = ProfileViewModel(profileRepository, spotifyController, authenticationController)

composeTestRule.setContent { EditProfileScreen(mockNavigationActions, viewModel) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ch.epfl.cs311.wanderwave.ui
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.lifecycle.viewModelScope
import androidx.test.ext.junit.runners.AndroidJUnit4
import ch.epfl.cs311.wanderwave.model.auth.AuthenticationController
import ch.epfl.cs311.wanderwave.model.data.Profile
import ch.epfl.cs311.wanderwave.model.remote.ProfileConnection
import ch.epfl.cs311.wanderwave.model.spotify.SpotifyController
Expand Down Expand Up @@ -51,10 +52,12 @@ class ProfileTest : TestCase(kaspressoBuilder = Kaspresso.Builder.withComposeSup

@RelaxedMockK private lateinit var spotifyController: SpotifyController

@RelaxedMockK private lateinit var authenticationController: AuthenticationController

@Before
fun setup() {
mockDependencies()
viewModel = ProfileViewModel(profileRepository, spotifyController)
viewModel = ProfileViewModel(profileRepository, spotifyController, authenticationController)

composeTestRule.setContent { ProfileScreen(mockNavigationActions, viewModel) }
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ch.epfl.cs311.wanderwave.viewmodel

import ch.epfl.cs311.wanderwave.model.auth.AuthenticationController
import ch.epfl.cs311.wanderwave.model.auth.AuthenticationUserData
import ch.epfl.cs311.wanderwave.model.data.Beacon
import ch.epfl.cs311.wanderwave.model.data.ListType
import ch.epfl.cs311.wanderwave.model.data.Location
Expand Down Expand Up @@ -47,6 +49,7 @@ class BeaconScreenViewModelTest {
@get:Rule val mockkRule = MockKRule(this)
@RelaxedMockK private lateinit var beaconConnection: BeaconConnection
@RelaxedMockK private lateinit var mockSpotifyController: SpotifyController
@RelaxedMockK private lateinit var mockAuthenticationController: AuthenticationController

lateinit var viewModel: BeaconViewModel
val testDispatcher = TestCoroutineDispatcher()
Expand All @@ -57,7 +60,14 @@ class BeaconScreenViewModelTest {
@Before
fun setup() {
Dispatchers.setMain(testDispatcher)
viewModel = BeaconViewModel(trackRepository, beaconRepository, mockSpotifyController)

every { mockAuthenticationController.isSignedIn() } returns true
every { mockAuthenticationController.getUserData() } returns
AuthenticationUserData("uid", "email", "name", "http://photoUrl/img.jpg")

viewModel =
BeaconViewModel(
trackRepository, beaconRepository, mockSpotifyController, mockAuthenticationController)
}

@OptIn(ExperimentalCoroutinesApi::class)
Expand All @@ -79,16 +89,19 @@ class BeaconScreenViewModelTest {
fun canConstructWithNoErrors() {
val connectResult = SpotifyController.ConnectResult.SUCCESS
every { mockSpotifyController.connectRemote() } returns flowOf(connectResult)
BeaconViewModel(trackRepository, beaconConnection, mockSpotifyController)
BeaconViewModel(
trackRepository, beaconConnection, mockSpotifyController, mockAuthenticationController)
}

@Test
fun addTrackToBeaconTest() {
val viewModel = BeaconViewModel(trackRepository, beaconConnection, mockSpotifyController)
val viewModel =
BeaconViewModel(
trackRepository, beaconConnection, mockSpotifyController, mockAuthenticationController)
val track = Track("trackId", "trackName", "trackArtist")
viewModel.addTrackToBeacon("beaconId", track, {})

verify { beaconConnection.addTrackToBeacon(any(), any(), any()) }
verify { beaconConnection.addTrackToBeacon(any(), any(), any(), any()) }
}

@OptIn(ExperimentalCoroutinesApi::class)
Expand Down Expand Up @@ -137,7 +150,9 @@ class BeaconScreenViewModelTest {

@Test
fun canSelectTracks() {
val viewModel = BeaconViewModel(trackRepository, beaconConnection, mockSpotifyController)
val viewModel =
BeaconViewModel(
trackRepository, beaconConnection, mockSpotifyController, mockAuthenticationController)
val track = Track("trackId", "trackName", "trackArtist")
viewModel.selectTrack(track)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ch.epfl.cs311.wanderwave.viewmodel

import androidx.test.ext.junit.runners.AndroidJUnit4
import ch.epfl.cs311.wanderwave.model.auth.AuthenticationController
import ch.epfl.cs311.wanderwave.model.data.ListType
import ch.epfl.cs311.wanderwave.model.data.Profile
import ch.epfl.cs311.wanderwave.model.data.Track
Expand Down Expand Up @@ -47,10 +48,12 @@ class ProfileViewModelTest {

@RelaxedMockK private lateinit var spotifyController: SpotifyController

@RelaxedMockK private lateinit var authenticationController: AuthenticationController

@Before
fun setup() {
Dispatchers.setMain(testDispatcher)
viewModel = ProfileViewModel(profileRepository, spotifyController)
viewModel = ProfileViewModel(profileRepository, spotifyController, authenticationController)
}

@After
Expand Down Expand Up @@ -199,7 +202,7 @@ class ProfileViewModelTest {
every { profileRepository.getItem(testId) } returns testFlow

// Act
viewModel.getProfileByID(testId)
viewModel.getProfileByID(testId, false)

// Assert
assertEquals(testProfile, viewModel.profile.value)
Expand All @@ -210,16 +213,21 @@ class ProfileViewModelTest {
val testFlowError = flowOf(Result.failure<Profile>(Exception("Test Exception")))
every { profileRepository.getItem(testId) } returns testFlowError

viewModel.getProfileByID(testId)
viewModel.getProfileByID(testId, false)
assertEquals(
ProfileViewModel.UIState(profile = null, isLoading = false, error = "Test Exception"),
viewModel.uiState.value)
}

@Test
fun emptyChildrenList_clearsChildrenPlaylistTrackList() = runBlockingTest {
fun testCreateProfile() = runBlockingTest {
every { profileRepository.getItem(any()) } returns
flowOf(Result.failure(Exception("Document does not exist")))

// Act
viewModel.emptyChildrenList()
viewModel.getProfileByID("firebaseUid", true)

verify { profileRepository.addItemWithId(any()) }

viewModel.getProfileByID("firebaseUid", false)
}
}
Loading

0 comments on commit f5a834c

Please sign in to comment.