Commit 155f6364 authored by Konstantin Tskhovrebov's avatar Konstantin Tskhovrebov 🤖

Merge branch 'feature/update_project_tests' into 'develop'

Feature/update project tests

See merge request terrakok/gitlab-client!178
parents bcdfde79 408d0d7d
package ru.terrakok.gitlabclient.model.interactor.auth
import io.reactivex.Single
import io.reactivex.observers.TestObserver
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import org.mockito.ArgumentMatchers
import org.mockito.Mockito.*
import ru.terrakok.gitlabclient.entity.TokenData
import ru.terrakok.gitlabclient.entity.app.session.OAuthParams
/**
* @author Artur Badretdinov (Gaket)
* 20.12.2016.
*/
class AuthInteractorTest {
private lateinit var interactor: AuthInteractor
private lateinit var authRepo: AuthRepository
private val HASH = "some_hash_here"
private val OAUTH_PARAMS =
OAuthParams("appId", "appKey", "redirect_url")
@Before
fun setUp() {
authRepo = mock()
interactor = AuthInteractor(
"some server path",
"some default server path",
authRepo,
HASH,
OAUTH_PARAMS,
projectCache = mock()
)
}
@Test
fun check_oauth_redirect() {
val testUrl = OAUTH_PARAMS.redirectUrl + "somepath"
val result = interactor.checkOAuthRedirect(testUrl)
Assert.assertTrue(result)
}
@Test
fun check_oauth_bad_redirect_path() {
val testUrl = "app://otherUrl/somepath"
val result = interactor.checkOAuthRedirect(testUrl)
Assert.assertFalse(result)
}
@Test
fun check_logout_cleans_token() {
interactor.logout()
verify(authRepo).clearAuthData()
}
@Test
fun is_signed_in() {
`when`(authRepo.isSignedIn).thenReturn(true)
val result = interactor.isSignedIn()
verify(authRepo).isSignedIn
Assert.assertTrue(result)
}
@Test
fun is_not_signed_in() {
`when`(authRepo.isSignedIn).thenReturn(false)
val result = interactor.isSignedIn()
verify(authRepo).isSignedIn
Assert.assertFalse(result)
}
@Test
fun refresh_token_correct_oauth() {
val code = "helloReader"
val testUrl = "http://something.com/test?code=" + code + "&state=happiness" + HASH
val tokenData = TokenData("", "", "", 0L, "")
`when`(authRepo.requestOAuthToken(
ArgumentMatchers.anyString(),
ArgumentMatchers.anyString(),
ArgumentMatchers.anyString(),
ArgumentMatchers.anyString())).thenReturn(Single.just(tokenData))
val testObserver: TestObserver<Void> = interactor.login(testUrl).test()
testObserver.awaitTerminalEvent()
verify(authRepo).requestOAuthToken(
OAUTH_PARAMS.appId,
OAUTH_PARAMS.appKey,
code,
OAUTH_PARAMS.redirectUrl)
testObserver
.assertNoValues()
.assertNoErrors()
}
@Test
fun refresh_token_incorrect_oauth() {
val testOauthRedirect = "There is no token"
val testObserver: TestObserver<Void> = interactor.login(testOauthRedirect).test()
testObserver.awaitTerminalEvent()
verifyNoMoreInteractions(authRepo)
testObserver
.assertNoValues()
.assertError(RuntimeException::class.java)
}
}
\ No newline at end of file
package ru.terrakok.gitlabclient.model.interactor.session
import com.nhaarman.mockito_kotlin.mock
import io.reactivex.Single
import io.reactivex.observers.TestObserver
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import org.mockito.ArgumentMatchers
import org.mockito.Mockito.`when`
import org.mockito.Mockito.verify
import ru.terrakok.gitlabclient.entity.app.session.OAuthParams
import ru.terrakok.gitlabclient.entity.app.session.UserAccount
import ru.terrakok.gitlabclient.model.data.cache.ProjectCache
import ru.terrakok.gitlabclient.model.repository.session.SessionRepository
/**
* @author Artur Badretdinov (Gaket)
* 20.12.2016.
*
* @author Vitaliy Belyaev on 18.05.2019.
*/
class SessionInteractorTest {
private lateinit var interactor: SessionInteractor
private lateinit var repository: SessionRepository
private lateinit var projectCache: ProjectCache
private val OAUTH_PARAMS =
OAuthParams("appId", "appKey", "redirect_url")
private val testAccount = UserAccount(
13L,
"token",
"user_server_path",
"user_avatar_url",
"user_name",
true)
@Before
fun setUp() {
repository = mock()
projectCache = mock()
interactor = SessionInteractor(
"some server path",
repository,
OAUTH_PARAMS,
projectCache
)
}
@Test
fun check_oauth_redirect() {
val testUrl = OAUTH_PARAMS.redirectUrl + "somepath"
val result = interactor.checkOAuthRedirect(testUrl)
Assert.assertTrue(result)
}
@Test
fun check_oauth_bad_redirect_path() {
val testUrl = "app://otherUrl/somepath"
val result = interactor.checkOAuthRedirect(testUrl)
Assert.assertFalse(result)
}
@Test
fun logout_of_current_user_with_other_account() {
`when`(repository.getCurrentUserAccount()).thenReturn(testAccount)
`when`(repository.logout(ArgumentMatchers.anyString())).thenReturn(testAccount)
val result = interactor.logout()
verify(repository).getCurrentUserAccount()
verify(repository).logout(testAccount.id)
Assert.assertTrue(result)
}
@Test
fun logout_of_current_user_without_other_account() {
`when`(repository.getCurrentUserAccount()).thenReturn(testAccount)
`when`(repository.logout(ArgumentMatchers.anyString())).thenReturn(null)
val result = interactor.logout()
verify(repository).getCurrentUserAccount()
verify(repository).logout(testAccount.id)
Assert.assertFalse(result)
}
@Test
fun logout_of_empty_current_user() {
`when`(repository.getCurrentUserAccount()).thenReturn(null)
val result = interactor.logout()
verify(repository).getCurrentUserAccount()
Assert.assertFalse(result)
}
@Test
fun logout_of_certain_user_with_other_account() {
`when`(repository.logout(ArgumentMatchers.anyString())).thenReturn(testAccount)
val result = interactor.logout(testAccount.id)
verify(projectCache).clear()
verify(repository).logout(testAccount.id)
Assert.assertTrue(result)
}
@Test
fun logout_of_certain_user_without_other_account() {
`when`(repository.logout(ArgumentMatchers.anyString())).thenReturn(null)
val result = interactor.logout(testAccount.id)
verify(projectCache).clear()
verify(repository).logout(testAccount.id)
Assert.assertFalse(result)
}
@Test
fun login_through_oauth_with_valid_hash() {
val code = "helloReader"
val hash = interactor.oauthUrl.substringAfterLast("=")
val testUrl = "http://something.com/test?code=$code&state=happiness$hash"
`when`(repository.login(
ArgumentMatchers.anyString(),
ArgumentMatchers.anyString(),
ArgumentMatchers.anyString(),
ArgumentMatchers.anyString())).thenReturn(Single.just(testAccount))
val testObserver: TestObserver<Void> = interactor.login(testUrl).test()
testObserver.awaitTerminalEvent()
verify(repository).login(
OAUTH_PARAMS.appId,
OAUTH_PARAMS.appKey,
code,
OAUTH_PARAMS.redirectUrl)
testObserver
.assertNoValues()
.assertNoErrors()
.assertComplete()
}
@Test
fun login_through_oauth_with_invalid_hash() {
val code = "helloReader"
val hash = "invalidHash"
val testUrl = "http://something.com/test?code=$code&state=happiness$hash"
val testObserver: TestObserver<Void> = interactor.login(testUrl).test()
testObserver.awaitTerminalEvent()
testObserver
.assertNoValues()
.assertError { it is RuntimeException }
.assertErrorMessage("Not valid oauth hash!")
}
@Test
fun login_through_custom_server_path() {
val customServerPath = "custom server path"
val privateToken = "private token"
`when`(repository.login(
ArgumentMatchers.anyString(),
ArgumentMatchers.anyString())).thenReturn(Single.just(testAccount))
val testObserver: TestObserver<Void> = interactor.login(customServerPath, privateToken).test()
testObserver.awaitTerminalEvent()
verify(repository).login(privateToken, customServerPath)
testObserver
.assertNoValues()
.assertNoErrors()
.assertComplete()
}
}
\ No newline at end of file
package ru.terrakok.gitlabclient.model.interactor.profile
package ru.terrakok.gitlabclient.model.repository.profile
import com.nhaarman.mockito_kotlin.mock
import io.reactivex.Single
import io.reactivex.observers.TestObserver
import org.junit.Before
import org.junit.Assert
import org.junit.Test
import org.mockito.Mockito.*
import org.mockito.Mockito.`when`
import org.mockito.Mockito.verify
import org.threeten.bp.LocalDateTime
import ru.terrakok.gitlabclient.TestSchedulers
import ru.terrakok.gitlabclient.entity.User
import ru.terrakok.gitlabclient.model.repository.profile.ProfileRepository
import java.util.*
import ru.terrakok.gitlabclient.model.data.server.GitlabApi
/**
* @author Artur Badretdinov (Gaket)
* 31.05.17.
*
* @author Vitaliy Belyaev on 18.05.19.
*
*/
class MyProfileInteractorTest {
class ProfileRepositoryTest {
private lateinit var profileRepo: ProfileRepository
private lateinit var testUser: User
private lateinit var testServer: String
private lateinit var testError: Throwable
@Before
fun setUp() {
val testDate = LocalDateTime.of(2018, 1, 1, 0, 0)
testUser = User(
private val testServer = "Test server"
private val testError = RuntimeException("test error")
private val testDate = LocalDateTime.of(2018, 1, 1, 0, 0)
private val testUser = User(
id = 1L,
username = "",
email = null,
......@@ -55,47 +51,43 @@ class MyProfileInteractorTest {
canCreateProject = false,
twoFactorEnabled = false,
external = false
)
)
testServer = "Test server"
profileRepo = mock(ProfileRepository::class.java)
testError = RuntimeException("test error")
}
private val api: GitlabApi = mock()
private val profileRepo = ProfileRepository(testServer, api, TestSchedulers())
@Test
fun get_user_info() {
val testUserInfo = MyUserInfo(testUser, testServer)
`when`(profileRepo.getMyProfile()).thenReturn(Single.just(testUser))
`when`(profileRepo.getMyServerName()).thenReturn(testServer)
fun get_user() {
`when`(api.getMyUser()).thenReturn(Single.just(testUser))
val interactor = MyProfileInteractor(profileRepo)
val testObserver: TestObserver<MyUserInfo> = interactor.getMyProfile().test()
val testObserver: TestObserver<User> = profileRepo.getMyProfile().test()
testObserver.awaitTerminalEvent()
verify(profileRepo).getMyProfile()
verify(profileRepo).getMyServerName()
verify(api).getMyUser()
testObserver
.assertValueCount(1)
.assertValue(testUserInfo)
.assertNoErrors()
.assertComplete()
.assertValue(testUser)
}
@Test
fun get_user_info_error() {
`when`(profileRepo.getMyProfile()).thenReturn(Single.error(testError))
`when`(profileRepo.getMyServerName()).thenReturn(testServer)
val interactor = MyProfileInteractor(profileRepo)
fun get_user_error() {
`when`(api.getMyUser()).thenReturn(Single.error(testError))
val testObserver: TestObserver<MyUserInfo> = interactor.getMyProfile().test()
val testObserver: TestObserver<User> = profileRepo.getMyProfile().test()
testObserver.awaitTerminalEvent()
verify(profileRepo).getMyProfile()
verify(api).getMyUser()
testObserver
.assertNoValues()
.assertError(testError)
}
@Test
fun get_my_server_name() {
val result = profileRepo.getMyServerName()
Assert.assertEquals(testServer, result)
}
}
\ No newline at end of file
......@@ -4,25 +4,31 @@ import com.nhaarman.mockito_kotlin.*
import io.reactivex.Single
import org.junit.Test
import ru.terrakok.gitlabclient.di.PrimitiveWrapper
import ru.terrakok.gitlabclient.entity.Color
import ru.terrakok.gitlabclient.entity.Label
import ru.terrakok.gitlabclient.model.interactor.label.LabelInteractor
import ru.terrakok.gitlabclient.model.system.flow.FlowRouter
import ru.terrakok.gitlabclient.presentation.global.ErrorHandler
/**
* @author Maxim Myalkin (MaxMyalkin) on 11.11.2018.
*
* @author Vitaliy Belyaev on 19.05.2019.
*/
class ProjectLabelsPresenterTest {
val projectId = 42L
private val projectId = 42L
val view: ProjectLabelsView = mock()
val interactor: LabelInteractor = mock()
val errorHandler: ErrorHandler = mock()
private val view: ProjectLabelsView = mock()
private val interactor: LabelInteractor = mock()
private val errorHandler: ErrorHandler = mock()
private val flowRouter: FlowRouter = mock()
val presenter = ProjectLabelsPresenter(
PrimitiveWrapper(projectId),
interactor,
errorHandler
private val presenter = ProjectLabelsPresenter(
PrimitiveWrapper(projectId),
interactor,
errorHandler,
flowRouter
)
@Test
......@@ -61,8 +67,8 @@ class ProjectLabelsPresenterTest {
val secondPageLabels = listOf(label(4), label(5), label(6))
whenever(interactor.getLabelList(eq(projectId), any()))
.thenReturn(Single.just(firstPageLabels))
.thenReturn(Single.just(secondPageLabels))
.thenReturn(Single.just(firstPageLabels))
.thenReturn(Single.just(secondPageLabels))
presenter.attachView(view)
......@@ -88,7 +94,7 @@ class ProjectLabelsPresenterTest {
val error = RuntimeException("error")
whenever(interactor.getLabelList(eq(projectId), any()))
.thenReturn(Single.error(error))
.thenReturn(Single.error(error))
presenter.attachView(view)
......@@ -107,8 +113,8 @@ class ProjectLabelsPresenterTest {
val error = RuntimeException("error")
whenever(interactor.getLabelList(eq(projectId), any()))
.thenReturn(Single.just(firstPageLabels))
.thenReturn(Single.error(error))
.thenReturn(Single.just(firstPageLabels))
.thenReturn(Single.error(error))
presenter.attachView(view)
presenter.loadNextLabelsPage()
......@@ -122,16 +128,15 @@ class ProjectLabelsPresenterTest {
private fun label(id: Long, subscribed: Boolean = false): Label {
return Label(
id = id,
name = "name$id",
color = "color$id",
description = null,
openIssuesCount = 0,
closedIssuesCount = 0,
openMergeRequestsCount = 0,
subscribed = subscribed,
priority = null
id = id,
name = "name$id",
color = Color("green", 12),
description = null,
openIssuesCount = 0,
closedIssuesCount = 0,
openMergeRequestsCount = 0,
subscribed = subscribed,
priority = null
)
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment