Skip to content

Commit

Permalink
Fix: SAML SSO fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RawanMatar89 committed Jul 9, 2024
1 parent d8ac592 commit 8c177a5
Show file tree
Hide file tree
Showing 8 changed files with 290 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,21 @@ public class SignInViewModel: ObservableObject {
}
}

@MainActor
func ssoLogin(title: String) async {

analytics.userSignInClicked()
isShowProgress = true
do {
let user = try await interactor.SSOlogin(jwtToken: "239i2oi3jrf2jflkj23lf2f")
analytics.identify(id: "\(user.id)", username: user.username, email: user.email)
analytics.userLogin(method: .password)
router.showMainOrWhatsNewScreen(sourceScreen: sourceScreen)
} catch let error {
failure(error)
}
}

@MainActor
func login(with result: Result<SocialAuthDetails, Error>) async {
switch result {
Expand Down
61 changes: 58 additions & 3 deletions Authorization/AuthorizationTests/AuthorizationMock.generated.swift

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,33 @@ final class SignInViewModelTests: XCTestCase {
XCTAssertEqual(viewModel.errorMessage, nil)
XCTAssertEqual(viewModel.isShowProgress, true)
}


func testSSOLoginSuccess() async throws {
let interactor = AuthInteractorProtocolMock()
let router = AuthorizationRouterMock()
let validator = Validator()
let analytics = AuthorizationAnalyticsMock()
let viewModel = SignInViewModel(
interactor: interactor,
router: router,
config: ConfigMock(),
analytics: analytics,
validator: validator,
sourceScreen: .default
)
let user = User(id: 1, username: "username", email: "[email protected]", name: "Name", userAvatar: "")

Given(interactor, .ssoLogin(title: .any, willReturn: user))

await viewModel.ssoLogin(title: "Riyadah")

Verify(interactor, 1, .ssoLogin(title: .any))
Verify(router, 1, .showMainOrWhatsNewScreen(sourceScreen: .any))

XCTAssertEqual(viewModel.errorMessage, nil)
XCTAssertEqual(viewModel.isShowProgress, true)
}

func testSocialLoginSuccess() async throws {
let interactor = AuthInteractorProtocolMock()
let router = AuthorizationRouterMock()
Expand Down
37 changes: 33 additions & 4 deletions Course/CourseTests/CourseMock.generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,24 @@ open class AuthInteractorProtocolMock: AuthInteractorProtocol, Mock {
}

@discardableResult
open func login(externalToken: String, backend: String) throws -> User {
open func SSOlogin(jwtToken: String) async throws -> Core.User {
addInvocation(.m_login__SSO__username_password(Parameter<String>.value(`jwtToken`)))
let perform = methodPerformValue(.m_login__SSO__username_password(Parameter<String>.value(`jwtToken`))) as? (String) -> Void
perform?(`jwtToken`)
var __value: User
do {
__value = try methodReturnValue(.m_login__SSO__username_password(Parameter<String>.value(jwtToken))).casted()
} catch MockError.notStubed {
onFatalFailure("Stub return value not specified for login(username: String, password: String). Use given")
Failure("Stub return value not specified for login(username: String, password: String). Use given")
} catch {
throw error
}
return __value
}

@discardableResult
open func login(externalToken: String, backend: String) throws -> User {
addInvocation(.m_login__externalToken_externalTokenbackend_backend(Parameter<String>.value(`externalToken`), Parameter<String>.value(`backend`)))
let perform = methodPerformValue(.m_login__externalToken_externalTokenbackend_backend(Parameter<String>.value(`externalToken`), Parameter<String>.value(`backend`))) as? (String, String) -> Void
perform?(`externalToken`, `backend`)
Expand Down Expand Up @@ -173,6 +190,7 @@ open class AuthInteractorProtocolMock: AuthInteractorProtocol, Mock {

fileprivate enum MethodType {
case m_login__username_usernamepassword_password(Parameter<String>, Parameter<String>)
case m_login__SSO__username_password(Parameter<String>)
case m_login__externalToken_externalTokenbackend_backend(Parameter<String>, Parameter<String>)
case m_resetPassword__email_email(Parameter<String>)
case m_getCookies__force_force(Parameter<Bool>)
Expand Down Expand Up @@ -223,6 +241,7 @@ open class AuthInteractorProtocolMock: AuthInteractorProtocol, Mock {
func intValue() -> Int {
switch self {
case let .m_login__username_usernamepassword_password(p0, p1): return p0.intValue + p1.intValue
case let .m_login__SSO__username_password(p0): return p0.intValue
case let .m_login__externalToken_externalTokenbackend_backend(p0, p1): return p0.intValue + p1.intValue
case let .m_resetPassword__email_email(p0): return p0.intValue
case let .m_getCookies__force_force(p0): return p0.intValue
Expand All @@ -234,6 +253,7 @@ open class AuthInteractorProtocolMock: AuthInteractorProtocol, Mock {
func assertionName() -> String {
switch self {
case .m_login__username_usernamepassword_password: return ".login(username:password:)"
case .m_login__SSO__username_password: return ".loginSSO(username:password:)"
case .m_login__externalToken_externalTokenbackend_backend: return ".login(externalToken:backend:)"
case .m_resetPassword__email_email: return ".resetPassword(email:)"
case .m_getCookies__force_force: return ".getCookies(force:)"
Expand Down Expand Up @@ -577,10 +597,16 @@ open class BaseRouterMock: BaseRouter, Mock {

open func showWebBrowser(title: String, url: URL) {
addInvocation(.m_showWebBrowser__title_titleurl_url(Parameter<String>.value(`title`), Parameter<URL>.value(`url`)))
let perform = methodPerformValue(.m_showWebBrowser__title_titleurl_url(Parameter<String>.value(`title`), Parameter<URL>.value(`url`))) as? (String, URL) -> Void
perform?(`title`, `url`)
let perform = methodPerformValue(.m_showWebBrowser__title_titleurl_url(Parameter<String>.value(`title`), Parameter<URL>.value(`url`))) as? (String, URL) -> Void
perform?(`title`, `url`)
}


open func showSSOWebBrowser(title: String) {
addInvocation(.m_showWebBrowser__SSO(Parameter<String>.value(`title`)))
let perform = methodPerformValue(.m_showWebBrowser__SSO(Parameter<String>.value(`title`))) as? (String) -> Void
perform?(`title`)
}

open func presentAlert(alertTitle: String, alertMessage: String, positiveAction: String, onCloseTapped: @escaping () -> Void, okTapped: @escaping () -> Void, type: AlertViewType) {
addInvocation(.m_presentAlert__alertTitle_alertTitlealertMessage_alertMessagepositiveAction_positiveActiononCloseTapped_onCloseTappedokTapped_okTappedtype_type(Parameter<String>.value(`alertTitle`), Parameter<String>.value(`alertMessage`), Parameter<String>.value(`positiveAction`), Parameter<() -> Void>.value(`onCloseTapped`), Parameter<() -> Void>.value(`okTapped`), Parameter<AlertViewType>.value(`type`)))
let perform = methodPerformValue(.m_presentAlert__alertTitle_alertTitlealertMessage_alertMessagepositiveAction_positiveActiononCloseTapped_onCloseTappedokTapped_okTappedtype_type(Parameter<String>.value(`alertTitle`), Parameter<String>.value(`alertMessage`), Parameter<String>.value(`positiveAction`), Parameter<() -> Void>.value(`onCloseTapped`), Parameter<() -> Void>.value(`okTapped`), Parameter<AlertViewType>.value(`type`))) as? (String, String, String, @escaping () -> Void, @escaping () -> Void, AlertViewType) -> Void
Expand Down Expand Up @@ -619,6 +645,7 @@ open class BaseRouterMock: BaseRouter, Mock {
case m_showForgotPasswordScreen
case m_showDiscoveryScreen__searchQuery_searchQuerysourceScreen_sourceScreen(Parameter<String?>, Parameter<LogistrationSourceScreen>)
case m_showWebBrowser__title_titleurl_url(Parameter<String>, Parameter<URL>)
case m_showWebBrowser__SSO(Parameter<String>)
case m_presentAlert__alertTitle_alertTitlealertMessage_alertMessagepositiveAction_positiveActiononCloseTapped_onCloseTappedokTapped_okTappedtype_type(Parameter<String>, Parameter<String>, Parameter<String>, Parameter<() -> Void>, Parameter<() -> Void>, Parameter<AlertViewType>)
case m_presentAlert__alertTitle_alertTitlealertMessage_alertMessagenextSectionName_nextSectionNameaction_actionimage_imageonCloseTapped_onCloseTappedokTapped_okTappednextSectionTapped_nextSectionTapped(Parameter<String>, Parameter<String>, Parameter<String?>, Parameter<String>, Parameter<SwiftUI.Image>, Parameter<() -> Void>, Parameter<() -> Void>, Parameter<() -> Void>)
case m_presentView__transitionStyle_transitionStyleview_viewcompletion_completion(Parameter<UIModalTransitionStyle>, Parameter<any View>, Parameter<(() -> Void)?>)
Expand Down Expand Up @@ -732,6 +759,7 @@ open class BaseRouterMock: BaseRouter, Mock {
case .m_showForgotPasswordScreen: return 0
case let .m_showDiscoveryScreen__searchQuery_searchQuerysourceScreen_sourceScreen(p0, p1): return p0.intValue + p1.intValue
case let .m_showWebBrowser__title_titleurl_url(p0, p1): return p0.intValue + p1.intValue
case let .m_showWebBrowser__SSO(p0): return p0.intValue
case let .m_presentAlert__alertTitle_alertTitlealertMessage_alertMessagepositiveAction_positiveActiononCloseTapped_onCloseTappedokTapped_okTappedtype_type(p0, p1, p2, p3, p4, p5): return p0.intValue + p1.intValue + p2.intValue + p3.intValue + p4.intValue + p5.intValue
case let .m_presentAlert__alertTitle_alertTitlealertMessage_alertMessagenextSectionName_nextSectionNameaction_actionimage_imageonCloseTapped_onCloseTappedokTapped_okTappednextSectionTapped_nextSectionTapped(p0, p1, p2, p3, p4, p5, p6, p7): return p0.intValue + p1.intValue + p2.intValue + p3.intValue + p4.intValue + p5.intValue + p6.intValue + p7.intValue
case let .m_presentView__transitionStyle_transitionStyleview_viewcompletion_completion(p0, p1, p2): return p0.intValue + p1.intValue + p2.intValue
Expand All @@ -752,6 +780,7 @@ open class BaseRouterMock: BaseRouter, Mock {
case .m_showForgotPasswordScreen: return ".showForgotPasswordScreen()"
case .m_showDiscoveryScreen__searchQuery_searchQuerysourceScreen_sourceScreen: return ".showDiscoveryScreen(searchQuery:sourceScreen:)"
case .m_showWebBrowser__title_titleurl_url: return ".showWebBrowser(title:url:)"
case .m_showWebBrowser__SSO: return ".showSSOWebBrowser(title:)"
case .m_presentAlert__alertTitle_alertTitlealertMessage_alertMessagepositiveAction_positiveActiononCloseTapped_onCloseTappedokTapped_okTappedtype_type: return ".presentAlert(alertTitle:alertMessage:positiveAction:onCloseTapped:okTapped:type:)"
case .m_presentAlert__alertTitle_alertTitlealertMessage_alertMessagenextSectionName_nextSectionNameaction_actionimage_imageonCloseTapped_onCloseTappedokTapped_okTappednextSectionTapped_nextSectionTapped: return ".presentAlert(alertTitle:alertMessage:nextSectionName:action:image:onCloseTapped:okTapped:nextSectionTapped:)"
case .m_presentView__transitionStyle_transitionStyleview_viewcompletion_completion: return ".presentView(transitionStyle:view:completion:)"
Expand Down
29 changes: 29 additions & 0 deletions Dashboard/DashboardTests/DashboardMock.generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,23 @@ open class AuthInteractorProtocolMock: AuthInteractorProtocol, Mock {
return __value
}

@discardableResult
open func SSOlogin(jwtToken: String) async throws -> Core.User {
addInvocation(.m_login__SSO__username_password(Parameter<String>.value(`jwtToken`)))
let perform = methodPerformValue(.m_login__SSO__username_password(Parameter<String>.value(`jwtToken`))) as? (String) -> Void
perform?(`jwtToken`)
var __value: User
do {
__value = try methodReturnValue(.m_login__SSO__username_password(Parameter<String>.value(jwtToken))).casted()
} catch MockError.notStubed {
onFatalFailure("Stub return value not specified for login(username: String, password: String). Use given")
Failure("Stub return value not specified for login(username: String, password: String). Use given")
} catch {
throw error
}
return __value
}

open func resetPassword(email: String) throws -> ResetPassword {
addInvocation(.m_resetPassword__email_email(Parameter<String>.value(`email`)))
let perform = methodPerformValue(.m_resetPassword__email_email(Parameter<String>.value(`email`))) as? (String) -> Void
Expand Down Expand Up @@ -173,6 +190,7 @@ open class AuthInteractorProtocolMock: AuthInteractorProtocol, Mock {

fileprivate enum MethodType {
case m_login__username_usernamepassword_password(Parameter<String>, Parameter<String>)
case m_login__SSO__username_password(Parameter<String>)
case m_login__externalToken_externalTokenbackend_backend(Parameter<String>, Parameter<String>)
case m_resetPassword__email_email(Parameter<String>)
case m_getCookies__force_force(Parameter<Bool>)
Expand Down Expand Up @@ -223,6 +241,7 @@ open class AuthInteractorProtocolMock: AuthInteractorProtocol, Mock {
func intValue() -> Int {
switch self {
case let .m_login__username_usernamepassword_password(p0, p1): return p0.intValue + p1.intValue
case let .m_login__SSO__username_password(p0): return p0.intValue
case let .m_login__externalToken_externalTokenbackend_backend(p0, p1): return p0.intValue + p1.intValue
case let .m_resetPassword__email_email(p0): return p0.intValue
case let .m_getCookies__force_force(p0): return p0.intValue
Expand All @@ -234,6 +253,7 @@ open class AuthInteractorProtocolMock: AuthInteractorProtocol, Mock {
func assertionName() -> String {
switch self {
case .m_login__username_usernamepassword_password: return ".login(username:password:)"
case .m_login__SSO__username_password: return ".loginSSO(username:password:)"
case .m_login__externalToken_externalTokenbackend_backend: return ".login(externalToken:backend:)"
case .m_resetPassword__email_email: return ".resetPassword(email:)"
case .m_getCookies__force_force: return ".getCookies(force:)"
Expand Down Expand Up @@ -581,6 +601,12 @@ open class BaseRouterMock: BaseRouter, Mock {
perform?(`title`, `url`)
}

open func showSSOWebBrowser(title: String) {
addInvocation(.m_showWebBrowser__SSO(Parameter<String>.value(`title`)))
let perform = methodPerformValue(.m_showWebBrowser__SSO(Parameter<String>.value(`title`))) as? (String) -> Void
perform?(`title`)
}

open func presentAlert(alertTitle: String, alertMessage: String, positiveAction: String, onCloseTapped: @escaping () -> Void, okTapped: @escaping () -> Void, type: AlertViewType) {
addInvocation(.m_presentAlert__alertTitle_alertTitlealertMessage_alertMessagepositiveAction_positiveActiononCloseTapped_onCloseTappedokTapped_okTappedtype_type(Parameter<String>.value(`alertTitle`), Parameter<String>.value(`alertMessage`), Parameter<String>.value(`positiveAction`), Parameter<() -> Void>.value(`onCloseTapped`), Parameter<() -> Void>.value(`okTapped`), Parameter<AlertViewType>.value(`type`)))
let perform = methodPerformValue(.m_presentAlert__alertTitle_alertTitlealertMessage_alertMessagepositiveAction_positiveActiononCloseTapped_onCloseTappedokTapped_okTappedtype_type(Parameter<String>.value(`alertTitle`), Parameter<String>.value(`alertMessage`), Parameter<String>.value(`positiveAction`), Parameter<() -> Void>.value(`onCloseTapped`), Parameter<() -> Void>.value(`okTapped`), Parameter<AlertViewType>.value(`type`))) as? (String, String, String, @escaping () -> Void, @escaping () -> Void, AlertViewType) -> Void
Expand Down Expand Up @@ -619,6 +645,7 @@ open class BaseRouterMock: BaseRouter, Mock {
case m_showForgotPasswordScreen
case m_showDiscoveryScreen__searchQuery_searchQuerysourceScreen_sourceScreen(Parameter<String?>, Parameter<LogistrationSourceScreen>)
case m_showWebBrowser__title_titleurl_url(Parameter<String>, Parameter<URL>)
case m_showWebBrowser__SSO(Parameter<String>)
case m_presentAlert__alertTitle_alertTitlealertMessage_alertMessagepositiveAction_positiveActiononCloseTapped_onCloseTappedokTapped_okTappedtype_type(Parameter<String>, Parameter<String>, Parameter<String>, Parameter<() -> Void>, Parameter<() -> Void>, Parameter<AlertViewType>)
case m_presentAlert__alertTitle_alertTitlealertMessage_alertMessagenextSectionName_nextSectionNameaction_actionimage_imageonCloseTapped_onCloseTappedokTapped_okTappednextSectionTapped_nextSectionTapped(Parameter<String>, Parameter<String>, Parameter<String?>, Parameter<String>, Parameter<SwiftUI.Image>, Parameter<() -> Void>, Parameter<() -> Void>, Parameter<() -> Void>)
case m_presentView__transitionStyle_transitionStyleview_viewcompletion_completion(Parameter<UIModalTransitionStyle>, Parameter<any View>, Parameter<(() -> Void)?>)
Expand Down Expand Up @@ -732,6 +759,7 @@ open class BaseRouterMock: BaseRouter, Mock {
case .m_showForgotPasswordScreen: return 0
case let .m_showDiscoveryScreen__searchQuery_searchQuerysourceScreen_sourceScreen(p0, p1): return p0.intValue + p1.intValue
case let .m_showWebBrowser__title_titleurl_url(p0, p1): return p0.intValue + p1.intValue
case let .m_showWebBrowser__SSO(p0): return p0.intValue
case let .m_presentAlert__alertTitle_alertTitlealertMessage_alertMessagepositiveAction_positiveActiononCloseTapped_onCloseTappedokTapped_okTappedtype_type(p0, p1, p2, p3, p4, p5): return p0.intValue + p1.intValue + p2.intValue + p3.intValue + p4.intValue + p5.intValue
case let .m_presentAlert__alertTitle_alertTitlealertMessage_alertMessagenextSectionName_nextSectionNameaction_actionimage_imageonCloseTapped_onCloseTappedokTapped_okTappednextSectionTapped_nextSectionTapped(p0, p1, p2, p3, p4, p5, p6, p7): return p0.intValue + p1.intValue + p2.intValue + p3.intValue + p4.intValue + p5.intValue + p6.intValue + p7.intValue
case let .m_presentView__transitionStyle_transitionStyleview_viewcompletion_completion(p0, p1, p2): return p0.intValue + p1.intValue + p2.intValue
Expand All @@ -752,6 +780,7 @@ open class BaseRouterMock: BaseRouter, Mock {
case .m_showForgotPasswordScreen: return ".showForgotPasswordScreen()"
case .m_showDiscoveryScreen__searchQuery_searchQuerysourceScreen_sourceScreen: return ".showDiscoveryScreen(searchQuery:sourceScreen:)"
case .m_showWebBrowser__title_titleurl_url: return ".showWebBrowser(title:url:)"
case .m_showWebBrowser__SSO: return ".showSSOWebBrowser(title:)"
case .m_presentAlert__alertTitle_alertTitlealertMessage_alertMessagepositiveAction_positiveActiononCloseTapped_onCloseTappedokTapped_okTappedtype_type: return ".presentAlert(alertTitle:alertMessage:positiveAction:onCloseTapped:okTapped:type:)"
case .m_presentAlert__alertTitle_alertTitlealertMessage_alertMessagenextSectionName_nextSectionNameaction_actionimage_imageonCloseTapped_onCloseTappedokTapped_okTappednextSectionTapped_nextSectionTapped: return ".presentAlert(alertTitle:alertMessage:nextSectionName:action:image:onCloseTapped:okTapped:nextSectionTapped:)"
case .m_presentView__transitionStyle_transitionStyleview_viewcompletion_completion: return ".presentView(transitionStyle:view:completion:)"
Expand Down
Loading

0 comments on commit 8c177a5

Please sign in to comment.