Files
suixinkan_ios_new/suixinkanTests/AuthModelsTests.swift
汉秋 5ab9b1b324 修复账号切换后「我的」页当前账号展示错误,并统一举报摄影师首页 URI。
切换账号时以用户选中账号为准写入展示标题与业务作用域,避免沿用旧门店名称;同时将首页举报摄影师入口 URI 对齐为 report_photographer。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-01 17:21:04 +08:00

159 lines
6.0 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// AuthModelsTests.swift
// suixinkanTests
//
// Created by Codex on 2026/6/26.
//
import XCTest
@testable import suixinkan
/// Auth v9
final class AuthModelsTests: XCTestCase {
/// fixture
func testV9AuthResponseDecodesMultiAccountFixture() throws {
let response = try TestFixture.payload(V9AuthResponse.self, named: "v9_login_multi_success")
XCTAssertEqual(response.token, "person-temp-token")
XCTAssertEqual(response.scenicUsers.count, 1)
XCTAssertEqual(response.storeUsers.count, 1)
XCTAssertEqual(response.accounts.map(\.businessUserId), [101, 201])
}
/// 使 ss_user_id ID
func testV9ScenicUserBusinessUserIdPrefersSsUserId() throws {
let json = """
{
"account_type": "scenic_user",
"id": 1,
"user_id": 2,
"scenic_user_id": 3,
"ss_user_id": 101,
"username": "scenic_admin",
"real_name": "",
"nickname": "",
"phone": "13800138000",
"scenic_id": 10,
"scenic_name": "",
"is_current": false
}
""".data(using: .utf8)!
let user = try JSONDecoder().decode(V9ScenicUser.self, from: json)
XCTAssertEqual(user.businessUserId, 101)
XCTAssertEqual(user.toAccountSwitchAccount().accountType, V9ScenicUser.accountTypeValue)
XCTAssertEqual(user.toAccountSwitchAccount().toSetUserRequest(), SetUserRequest(ssUserId: 101))
}
/// 使 store_user_id ID
func testV9StoreUserBusinessUserIdPrefersStoreUserId() throws {
let json = """
{
"account_type": "store_user",
"id": 1,
"user_id": 2,
"store_user_id": 201,
"username": "store_admin",
"user_name": "store_admin",
"real_name": "",
"phone": "13800138000",
"avatar": "",
"scenic_id": 10,
"scenic_name": "",
"store_id": 20,
"store_name": "",
"is_current": false
}
""".data(using: .utf8)!
let user = try JSONDecoder().decode(V9StoreUser.self, from: json)
XCTAssertEqual(user.businessUserId, 201)
XCTAssertEqual(user.toAccountSwitchAccount().accountType, V9StoreUser.accountTypeValue)
XCTAssertEqual(user.toAccountSwitchAccount().toSetUserRequest(), SetUserRequest(storeUserId: 201))
}
///
func testV9AuthResponseDerivesScopesFromAccounts() throws {
let response = try TestFixture.payload(V9AuthResponse.self, named: "v9_login_multi_success")
XCTAssertEqual(response.scenicScopes.map(\.id), [10])
XCTAssertEqual(response.storeScopes.map(\.id), [20])
XCTAssertEqual(response.primaryProfile?.displayName, "示例门店")
}
/// 使 isCurrent
func testV9AuthResponsePreferredScopeSelectionUsesCurrentStoreAccount() throws {
let scenicJSON = """
{
"account_type": "scenic_user",
"id": 101,
"user_id": 101,
"scenic_user_id": 101,
"ss_user_id": 101,
"username": "scenic_admin",
"real_name": "",
"nickname": "",
"phone": "13800138000",
"scenic_id": 1,
"scenic_name": "",
"is_current": false
}
"""
let storeJSON = """
{
"account_type": "store_user",
"id": 201,
"user_id": 201,
"store_user_id": 201,
"username": "store_admin",
"user_name": "store_admin",
"real_name": "",
"phone": "13800138000",
"avatar": "",
"scenic_id": 2,
"scenic_name": "",
"store_id": 20,
"store_name": "",
"is_current": true
}
"""
let scenicUser = try JSONDecoder().decode(V9ScenicUser.self, from: Data(scenicJSON.utf8))
let storeUser = try JSONDecoder().decode(V9StoreUser.self, from: Data(storeJSON.utf8))
let response = V9AuthResponse(token: "token", scenicUsers: [scenicUser], storeUsers: [storeUser])
XCTAssertEqual(response.currentAccountIdentity?.accountType, V9StoreUser.accountTypeValue)
XCTAssertEqual(response.currentAccountIdentity?.businessUserId, 201)
XCTAssertEqual(response.preferredScopeSelection.scenicId, 2)
XCTAssertEqual(response.preferredScopeSelection.storeId, 20)
let context = AccountContext()
context.replaceScopes(
scenic: [BusinessScope(id: 1, name: "旧景区", kind: .scenic)],
stores: [],
currentScenicId: 1,
currentStoreId: nil
)
context.replaceScopes(
scenic: response.scenicScopes,
stores: response.storeScopes,
currentScenicId: response.preferredScopeSelection.scenicId,
currentStoreId: response.preferredScopeSelection.storeId
)
XCTAssertEqual(context.currentScenic?.id, 2)
XCTAssertEqual(context.currentStore?.id, 20)
}
/// LoginRequest
func testLoginRequestEncodesExpectedDefaults() throws {
let request = LoginRequest(username: "18651857230", password: "secret123")
let json = try JSONSerialization.jsonObject(with: JSONEncoder().encode(request)) as? [String: Any]
XCTAssertEqual(json?["username"] as? String, "18651857230")
XCTAssertEqual(json?["password"] as? String, "secret123")
XCTAssertEqual(json?["type"] as? Int, 1)
XCTAssertEqual(json?["mobile"] as? String, "")
XCTAssertEqual(json?["code"] as? String, "")
}
}