Skip to content

Commit 17b05dc

Browse files
authored
fix: Completed Unit test of SignUpPresenter (#2492)
1 parent ed89462 commit 17b05dc

File tree

3 files changed

+112
-4
lines changed

3 files changed

+112
-4
lines changed

app/src/main/java/org/fossasia/susi/ai/di/Modules.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package org.fossasia.susi.ai.di
33
import org.fossasia.susi.ai.chat.ChatPresenter
44
import org.fossasia.susi.ai.chat.contract.IChatPresenter
55
import org.fossasia.susi.ai.chat.contract.IChatView
6+
import org.fossasia.susi.ai.data.ForgotPasswordModel
67
import org.fossasia.susi.ai.data.GroupWiseSkillsModel
78
import org.fossasia.susi.ai.data.LoginModel
89
import org.fossasia.susi.ai.data.SettingModel
@@ -50,7 +51,7 @@ val modules: Module = module(override = true) {
5051

5152
factory<ILoginPresenter> { (view: ILoginView) -> LoginPresenter(androidContext(), view) }
5253

53-
factory<ISignUpPresenter> { (view: ISignUpView) -> SignUpPresenter(get(), UtilModel(androidContext()), get(), view) }
54+
factory<ISignUpPresenter> { (view: ISignUpView) -> SignUpPresenter(get(), get(), UtilModel(androidContext()), get(), view) }
5455

5556
single {
5657
DatabaseRepository() as IDatabaseRepository
@@ -60,6 +61,10 @@ val modules: Module = module(override = true) {
6061
SignUpModel()
6162
}
6263

64+
single {
65+
ForgotPasswordModel()
66+
}
67+
6368
factory<ISettingsPresenter> { (view: ISettingsView) -> SettingsPresenter(androidContext(), view) }
6469

6570
factory<ISkillDetailsPresenter> { (view: ISkillDetailsView) -> SkillDetailsPresenter(androidContext(), view) }

app/src/main/java/org/fossasia/susi/ai/signup/SignUpPresenter.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,10 @@ import retrofit2.Response
2626
* Created by mayanktripathi on 05/07/17.
2727
*/
2828

29-
class SignUpPresenter(private val signUpModel: SignUpModel, private val utilModel: UtilModel, private val databaseRepository: IDatabaseRepository, private val signUpView: ISignUpView) : ISignUpPresenter, ISignUpModel.OnSignUpFinishedListener, IForgotPasswordModel.OnFinishListener {
29+
class SignUpPresenter(private val forgotPasswordModel: ForgotPasswordModel, private val signUpModel: SignUpModel, private val utilModel: UtilModel, private val databaseRepository: IDatabaseRepository, private val signUpView: ISignUpView) : ISignUpPresenter, ISignUpModel.OnSignUpFinishedListener, IForgotPasswordModel.OnFinishListener {
3030

3131
private var settingView: ISettingsView? = null
3232
lateinit var email: String
33-
var forgotPasswordModel: ForgotPasswordModel = ForgotPasswordModel()
3433

3534
override fun signUp(email: String, password: String, conpass: String, isSusiServerSelected: Boolean, url: String, isTermsAndConditionSelected: Boolean) {
3635

app/src/test/java/org/fossasia/susi/ai/signup/SignUpPresenterTest.kt

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,36 @@
11
package org.fossasia.susi.ai.signup
22

3+
import android.graphics.Color
4+
import okhttp3.ResponseBody
5+
import org.fossasia.susi.ai.R
6+
import org.fossasia.susi.ai.data.ForgotPasswordModel
37
import org.fossasia.susi.ai.data.SignUpModel
48
import org.fossasia.susi.ai.data.UtilModel
59
import org.fossasia.susi.ai.data.db.DatabaseRepository
610
import org.fossasia.susi.ai.data.db.contract.IDatabaseRepository
711
import org.fossasia.susi.ai.helper.Constant
12+
import org.fossasia.susi.ai.rest.responses.susi.ForgotPasswordResponse
13+
import org.fossasia.susi.ai.rest.responses.susi.SignUpResponse
814
import org.fossasia.susi.ai.signup.contract.ISignUpView
15+
import org.junit.Before
916
import org.junit.Test
1017
import org.mockito.Mock
18+
import org.mockito.Mockito.`when`
1119
import org.mockito.Mockito.mock
1220
import org.mockito.Mockito.times
1321
import org.mockito.Mockito.verify
1422
class SignUpPresenterTest {
23+
private val ERROR_INTERNET_CONNECTIVITY = "Internet Connectivity Problem."
24+
private val NO_INTERNET_AVAILABLE = "Internet Connection Not Available."
25+
private val UNKNOWN_HOST_EXPRESSINON = "Unknown Host Exception"
26+
27+
private val responseSuccess: retrofit2.Response<SignUpResponse> = retrofit2.Response.success(SignUpResponse("Message", null))
28+
private val responseCode422: retrofit2.Response<SignUpResponse> = retrofit2.Response.error(422, ResponseBody.create(null, "content"))
29+
private val responseCodeUnknown: retrofit2.Response<SignUpResponse> = retrofit2.Response.error(500, ResponseBody.create(null, "content"))
30+
31+
private val forgotResponseSuccess: retrofit2.Response<ForgotPasswordResponse> = retrofit2.Response.success(ForgotPasswordResponse("Message"))
32+
private val forgotResponse422: retrofit2.Response<ForgotPasswordResponse> = retrofit2.Response.error(422, ResponseBody.create(null, "content"))
33+
private val forgotResponseUnknown: retrofit2.Response<ForgotPasswordResponse> = retrofit2.Response.error(500, ResponseBody.create(null, "content"))
1534

1635
@Mock
1736
val signUpView: ISignUpView = mock(SignUpActivity::class.java)
@@ -21,8 +40,17 @@ class SignUpPresenterTest {
2140
val utilModel: UtilModel = mock(UtilModel::class.java)
2241
@Mock
2342
val iSignUpModel: SignUpModel = mock(SignUpModel::class.java)
43+
@Mock
44+
val forgotPasswordModel: ForgotPasswordModel = mock(ForgotPasswordModel::class.java)
2445

25-
val signUpPresenter: SignUpPresenter = SignUpPresenter(iSignUpModel, utilModel, databaseRepository, signUpView)
46+
val signUpPresenter: SignUpPresenter = SignUpPresenter(forgotPasswordModel, iSignUpModel, utilModel, databaseRepository, signUpView)
47+
48+
@Before
49+
fun setUp() {
50+
`when`(utilModel.getString(R.string.error_internet_connectivity)).thenReturn(ERROR_INTERNET_CONNECTIVITY)
51+
`when`(utilModel.getString(R.string.no_internet_connection)).thenReturn(NO_INTERNET_AVAILABLE)
52+
`when`(utilModel.getString(R.string.unknown_host_exception)).thenReturn(UNKNOWN_HOST_EXPRESSINON)
53+
}
2654

2755
@Test
2856
fun signUpCredentialsTest() {
@@ -73,4 +101,80 @@ class SignUpPresenterTest {
73101
verify(signUpView).showProgress(true)
74102
verify(iSignUpModel).signUp("[email protected]", "P@ssword12", signUpPresenter)
75103
}
104+
105+
@Test
106+
fun shouldShowErrorOnFailure() {
107+
signUpPresenter.onError(Throwable())
108+
verify(signUpView).showProgress(false)
109+
verify(signUpView).onSignUpError(utilModel.getString(R.string.error_internet_connectivity),
110+
utilModel.getString(R.string.no_internet_connection))
111+
}
112+
113+
@Test
114+
fun shouldShowSuccessOnSuccess() {
115+
signUpPresenter.onSuccess(responseSuccess)
116+
verify(signUpView, times(2)).showProgress(false)
117+
verify(signUpView).alertSuccess()
118+
}
119+
120+
@Test
121+
fun shouldShowErrorOnResponse422() {
122+
signUpPresenter.onSuccess(responseCode422)
123+
verify(signUpView, times(2)).showProgress(false)
124+
verify(signUpView).alertFailure()
125+
}
126+
127+
@Test
128+
fun shouldShowSignUpError_onUnknownResponseCode() {
129+
signUpPresenter.onSuccess(responseCodeUnknown)
130+
verify(signUpView, times(2)).showProgress(false)
131+
verify(signUpView).onSignUpError("${responseCodeUnknown.code()} " + utilModel.getString(R.string.error), responseCodeUnknown.message())
132+
}
133+
134+
@Test
135+
fun requestPasswordTest() {
136+
signUpPresenter.requestPassword("", "", true)
137+
verify(signUpView).invalidCredentials(true, Constant.EMAIL)
138+
139+
signUpPresenter.requestPassword("email", "", true)
140+
verify(signUpView).invalidCredentials(false, Constant.EMAIL)
141+
142+
signUpPresenter.requestPassword("[email protected]", "", true)
143+
verify(signUpView).invalidCredentials(true, Constant.INPUT_URL)
144+
145+
signUpPresenter.requestPassword("[email protected]", "url", true)
146+
verify(signUpView).invalidCredentials(false, Constant.INPUT_URL)
147+
148+
signUpPresenter.requestPassword("[email protected]", "https://github.com", true)
149+
verify(utilModel).setServer(false)
150+
verify(utilModel).setCustomURL("https://github.com")
151+
verify(signUpView).showForgotPasswordProgress(true)
152+
verify(forgotPasswordModel).requestPassword("[email protected]", signUpPresenter)
153+
154+
signUpPresenter.requestPassword("[email protected]", "https://github.com", false)
155+
utilModel.setServer(true)
156+
verify(signUpView, times(2)).showForgotPasswordProgress(true)
157+
verify(forgotPasswordModel, times(2)).requestPassword("[email protected]", signUpPresenter)
158+
}
159+
160+
@Test
161+
fun forgotResponseSuccessTest() {
162+
signUpPresenter.onForgotPasswordModelSuccess(forgotResponseSuccess)
163+
verify(signUpView).showForgotPasswordProgress(false)
164+
verify(signUpView).resetPasswordSuccess()
165+
}
166+
167+
@Test
168+
fun forgotResponseCode422() {
169+
signUpPresenter.onForgotPasswordModelSuccess(forgotResponse422)
170+
verify(signUpView).resetPasswordFailure(utilModel.getString(R.string.email_invalid_title), utilModel.getString(R.string.email_invalid), utilModel.getString(R.string.retry), Color.RED)
171+
verify(signUpView).showForgotPasswordProgress(false)
172+
}
173+
174+
@Test
175+
fun forgotResponseUnknown() {
176+
signUpPresenter.onForgotPasswordModelSuccess(forgotResponseUnknown)
177+
verify(signUpView).showForgotPasswordProgress(false)
178+
verify(signUpView).resetPasswordFailure("${forgotResponseUnknown.code()} " + utilModel.getString(R.string.error), forgotResponseUnknown.message(), utilModel.getString(R.string.ok), Color.BLUE)
179+
}
76180
}

0 commit comments

Comments
 (0)