11package 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
37import org.fossasia.susi.ai.data.SignUpModel
48import org.fossasia.susi.ai.data.UtilModel
59import org.fossasia.susi.ai.data.db.DatabaseRepository
610import org.fossasia.susi.ai.data.db.contract.IDatabaseRepository
711import 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
814import org.fossasia.susi.ai.signup.contract.ISignUpView
15+ import org.junit.Before
916import org.junit.Test
1017import org.mockito.Mock
18+ import org.mockito.Mockito.`when`
1119import org.mockito.Mockito.mock
1220import org.mockito.Mockito.times
1321import org.mockito.Mockito.verify
1422class 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