Skip to content

Commit

Permalink
✅ SignUpWorkerTests 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
loinsir committed Jan 10, 2024
1 parent 9468a9e commit cd5a149
Show file tree
Hide file tree
Showing 4 changed files with 248 additions and 13 deletions.
4 changes: 4 additions & 0 deletions iOS/Layover/Layover.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
19B665D92B4EEDDD0083E63C /* SignUpWorkerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19B665D52B4EEDDD0083E63C /* SignUpWorkerTests.swift */; };
19B665DA2B4EEDDD0083E63C /* SignUpInteractorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19B665D62B4EEDDD0083E63C /* SignUpInteractorTests.swift */; };
19B665DB2B4EEDDD0083E63C /* SignUpPresenterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19B665D72B4EEDDD0083E63C /* SignUpPresenterTests.swift */; };
19B665DD2B4F0A7B0083E63C /* MockSignUpWorker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19B665DC2B4F0A7B0083E63C /* MockSignUpWorker.swift */; };
19C7AFCE2B02410F003B35F2 /* AuthManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19C7AFCD2B02410F003B35F2 /* AuthManager.swift */; };
19C7AFD62B02584D003B35F2 /* KeychainStored.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19C7AFD52B02584D003B35F2 /* KeychainStored.swift */; };
19E79AC02B0A85D0009EA9ED /* LoopingPlayerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19E79ABF2B0A85D0009EA9ED /* LoopingPlayerView.swift */; };
Expand Down Expand Up @@ -376,6 +377,7 @@
19B665D52B4EEDDD0083E63C /* SignUpWorkerTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SignUpWorkerTests.swift; sourceTree = "<group>"; };
19B665D62B4EEDDD0083E63C /* SignUpInteractorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SignUpInteractorTests.swift; sourceTree = "<group>"; };
19B665D72B4EEDDD0083E63C /* SignUpPresenterTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SignUpPresenterTests.swift; sourceTree = "<group>"; };
19B665DC2B4F0A7B0083E63C /* MockSignUpWorker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockSignUpWorker.swift; sourceTree = "<group>"; };
19C7AFCD2B02410F003B35F2 /* AuthManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthManager.swift; sourceTree = "<group>"; };
19C7AFD52B02584D003B35F2 /* KeychainStored.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeychainStored.swift; sourceTree = "<group>"; };
19E79ABF2B0A85D0009EA9ED /* LoopingPlayerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoopingPlayerView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -656,6 +658,7 @@
1925136C2B26F84E001533FA /* MockTagPlayListWorker.swift */,
192513842B27852C001533FA /* MockUserWorker.swift */,
19AE481B2B28C53800DD4612 /* MockSettingWorker.swift */,
19B665DC2B4F0A7B0083E63C /* MockSignUpWorker.swift */,
);
path = Workers;
sourceTree = "<group>";
Expand Down Expand Up @@ -1600,6 +1603,7 @@
files = (
192513852B27852C001533FA /* MockUserWorker.swift in Sources */,
192513832B277CD7001533FA /* ProfilePresenterTests.swift in Sources */,
19B665DD2B4F0A7B0083E63C /* MockSignUpWorker.swift in Sources */,
192513812B277CD7001533FA /* ProfileInteractorTests.swift in Sources */,
194C21C42B1DEE6B00C62645 /* HomeInteractorTests.swift in Sources */,
FC4E0C0F2B282AE500152596 /* UploadPostPresenterTests.swift in Sources */,
Expand Down
85 changes: 85 additions & 0 deletions iOS/Layover/LayoverTests/Mocks/Workers/MockSignUpWorker.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//
// MockSignUpWorker.swift
// LayoverTests
//
// Created by 김인환 on 1/11/24.
// Copyright © 2024 CodeBomber. All rights reserved.
//

@testable import Layover
import Foundation
import OSLog

class MockSignUpWorker: SignUpWorkerProtocol {

// MARK: - Properties

private let provider: ProviderType

// MARK: - Initializer

init(provider: ProviderType = Provider(session: .initMockSession(),
authManager: StubAuthManager())) {
self.provider = provider
}

func signUp(withKakao socialToken: String, username: String) async -> Bool {
guard let mockFileLocation = Bundle(for: type(of: self)).url(forResource: "LoginData", withExtension: "json"),
let mockData = try? Data(contentsOf: mockFileLocation) else {
return false
}

MockURLProtocol.requestHandler = { request in
let response = HTTPURLResponse(url: request.url!,
statusCode: 200,
httpVersion: nil,
headerFields: nil)
return (response, mockData, nil)
}

do {
var bodyParameters = [String: String]()
bodyParameters.updateValue(socialToken, forKey: "accessToken")
bodyParameters.updateValue(username, forKey: "username")

let endPoint = EndPoint<Response<LoginDTO>>(path: "/oauth/signup/kakao",
method: .POST,
bodyParameters: bodyParameters)
let response = try await provider.request(with: endPoint, authenticationIfNeeded: false, retryCount: 0)
return true
} catch {
os_log(.error, log: .data, "%@", error.localizedDescription)
return false
}
}

func signUp(withApple identityToken: String, username: String) async -> Bool {
guard let fileLocation: URL = Bundle(for: type(of: self)).url(forResource: "LoginData", withExtension: "json") else {
return false
}
guard let mockData: Data = try? Data(contentsOf: fileLocation) else {
return false
}
MockURLProtocol.requestHandler = { request in
let response = HTTPURLResponse(url: request.url!,
statusCode: 200,
httpVersion: nil,
headerFields: nil)
return (response, mockData, nil)
}
do {
var bodyParameters: [String: String] = [:]
bodyParameters.updateValue(identityToken, forKey: "identityToken")
bodyParameters.updateValue(username, forKey: "username")

let endPoint = EndPoint<Response<LoginDTO>>(path: "/oauth/signup/apple",
method: .POST,
bodyParameters: bodyParameters)
let response = try await provider.request(with: endPoint, authenticationIfNeeded: false, retryCount: 0)
return true
} catch {
os_log(.error, log: .data, "%@", error.localizedDescription)
return false
}
}
}
151 changes: 140 additions & 11 deletions iOS/Layover/LayoverTests/Scenes/SignUp/SignUpWorkerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
@testable import Layover
import XCTest

class SignUpWorkerTests: XCTestCase
{
final class SignUpWorkerTests: XCTestCase {
// MARK: Subject under test

var sut: SignUpWorker!

var authManagerSpy: AuthManagerSpy!

// MARK: - Test lifecycle

override func setUp() {
Expand All @@ -31,20 +31,149 @@ class SignUpWorkerTests: XCTestCase
}

// MARK: - Test setup

func setupSignUpWorker() {
sut = SignUpWorker()
authManagerSpy = AuthManagerSpy()
sut = SignUpWorker(provider: Provider(session: .initMockSession(), authManager: StubAuthManager()),
authManager: authManagerSpy)
}

// MARK: - Test doubles
// MARK: - Test Doubles

final class AuthManagerSpy: StubAuthManager {
var loginCalled = false
var logoutCalled = false

override init() {
super.init()
self.accessToken = nil
self.refreshToken = nil
self.loginType = nil
self.isLoggedIn = nil
}

override func login(accessToken: String?, refreshToken: String?, loginType: LoginType?) {
loginCalled = true
super.login(accessToken: accessToken, refreshToken: refreshToken, loginType: loginType)
}

override func logout() {
logoutCalled = true
super.logout()
}
}

// MARK: - Tests

func testSomething() {
// Given

// When
func test_카카오_회원가입을_성공적으로_했을때_true를_반환하고_로그인_처리된다() async {
// arrange
guard let mockFileLocation = Bundle(for: type(of: self)).url(forResource: "LoginData", withExtension: "json"),
let mockData = try? Data(contentsOf: mockFileLocation) else {
XCTFail("Mock json 파일 로드 실패.")
return
}

MockURLProtocol.requestHandler = { request in
let response = HTTPURLResponse(url: request.url!,
statusCode: 200,
httpVersion: nil,
headerFields: nil)
return (response, mockData, nil)
}

// act
let isSuccess = await sut.signUp(withKakao: "MockToken", username: "안유진")

// assert
XCTAssertTrue(isSuccess)
XCTAssertTrue(authManagerSpy.loginCalled)
XCTAssertEqual(authManagerSpy.accessToken, "mockAccessToken")
XCTAssertEqual(authManagerSpy.refreshToken, "mockRefreshToken")
XCTAssertEqual(authManagerSpy.loginType, .kakao)
XCTAssertTrue(authManagerSpy.isLoggedIn == true)
}

func test_카카오_회원가입을_실패했을때_false를_반환하고_로그인_처리되지_않는다() async {
// arrange
guard let mockFileLocation = Bundle(for: type(of: self)).url(forResource: "LoginData", withExtension: "json"),
let mockData = try? Data(contentsOf: mockFileLocation) else {
XCTFail("Mock json 파일 로드 실패.")
return
}

MockURLProtocol.requestHandler = { request in
let response = HTTPURLResponse(url: request.url!,
statusCode: 400,
httpVersion: nil,
headerFields: nil)
return (response, mockData, nil)
}

// act
let isSuccess = await sut.signUp(withKakao: "MockToken", username: "안유진")

// assert
XCTAssertFalse(isSuccess)
XCTAssertFalse(authManagerSpy.loginCalled)
XCTAssertNil(authManagerSpy.accessToken)
XCTAssertNil(authManagerSpy.refreshToken)
XCTAssertNil(authManagerSpy.loginType)
XCTAssertFalse(authManagerSpy.isLoggedIn == true)
}

func test_애플_회원가입을_성공적으로_했을때_true를_반환하고_로그인_처리된다() async {
// arrange
guard let mockFileLocation = Bundle(for: type(of: self)).url(forResource: "LoginData", withExtension: "json"),
let mockData = try? Data(contentsOf: mockFileLocation) else {
XCTFail("Mock json 파일 로드 실패.")
return
}

MockURLProtocol.requestHandler = { request in
let response = HTTPURLResponse(url: request.url!,
statusCode: 200,
httpVersion: nil,
headerFields: nil)
return (response, mockData, nil)
}

// act
let isSuccess = await sut.signUp(withApple: "MockToken", username: "안유진")

// assert
XCTAssertTrue(isSuccess)
XCTAssertTrue(authManagerSpy.loginCalled)
XCTAssertEqual(authManagerSpy.accessToken, "mockAccessToken")
XCTAssertEqual(authManagerSpy.refreshToken, "mockRefreshToken")
XCTAssertEqual(authManagerSpy.loginType, .apple)
XCTAssertTrue(authManagerSpy.isLoggedIn == true)
}

func test_애플_회원가입을_실패했을때_false를_반환하고_로그인_처리되지_않는다() async {
// arrange
guard let mockFileLocation = Bundle(for: type(of: self)).url(forResource: "LoginData", withExtension: "json"),
let mockData = try? Data(contentsOf: mockFileLocation) else {
XCTFail("Mock json 파일 로드 실패.")
return
}

MockURLProtocol.requestHandler = { request in
let response = HTTPURLResponse(url: request.url!,
statusCode: 400,
httpVersion: nil,
headerFields: nil)
return (response, mockData, nil)
}

// act
let isSuccess = await sut.signUp(withApple: "MockToken", username: "안유진")

// Then
// assert
XCTAssertFalse(isSuccess)
XCTAssertFalse(authManagerSpy.loginCalled)
XCTAssertNil(authManagerSpy.accessToken)
XCTAssertNil(authManagerSpy.refreshToken)
XCTAssertNil(authManagerSpy.loginType)
XCTAssertFalse(authManagerSpy.isLoggedIn == true)
}
}
21 changes: 19 additions & 2 deletions iOS/Layover/LayoverTests/Stubs/StubAuthManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,30 @@
@testable import Layover
import Foundation

final class StubAuthManager: AuthManagerProtocol {
class StubAuthManager: AuthManagerProtocol {

// MARK: Properties
// MARK: - Properties

var accessToken: String? = "Fake Access Token"
var refreshToken: String? = "Fake Refresh Token"
var isLoggedIn: Bool? = true
var loginType: LoginType? = .kakao
var memberID: Int? = -1

// MARK: - Methods

func login(accessToken: String?, refreshToken: String?, loginType: LoginType?) {
self.accessToken = accessToken
self.refreshToken = refreshToken
self.loginType = loginType
isLoggedIn = true
}

func logout() {
accessToken = nil
refreshToken = nil
memberID = nil
loginType = nil
isLoggedIn = false
}
}

0 comments on commit cd5a149

Please sign in to comment.