Files
suixinkan_uikit/suixinkanTests/AuthModelsTests.swift

135 lines
4.3 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
//
import XCTest
@testable import suixinkan
/// Auth v9
final class AuthModelsTests: XCTestCase {
func testV9AuthResponseDecodesMultiAccountJSON() throws {
let json = """
{
"code": 100000,
"msg": "success",
"data": {
"token": "person-temp-token",
"scenic_users": [
{
"account_type": "scenic_user",
"id": 101,
"user_id": 101,
"scenic_user_id": 101,
"ss_user_id": 101,
"username": "scenic_admin",
"user_name": "scenic_operator",
"real_name": "",
"nickname": "",
"phone": "13800138000",
"scenic_id": 10,
"scenic_name": "",
"is_current": false
}
],
"store_users": [
{
"account_type": "store_user",
"id": 201,
"user_id": 201,
"store_user_id": 201,
"username": "store_admin",
"user_name": "store_admin",
"real_name": "",
"phone": "13900139000",
"avatar": "",
"scenic_id": 10,
"scenic_name": "",
"store_id": 20,
"store_name": "",
"is_current": false
}
]
}
}
""".data(using: .utf8)!
let envelope = try JSONDecoder().decode(APIEnvelope<V9AuthResponse>.self, from: json)
let response = try XCTUnwrap(envelope.data)
XCTAssertEqual(response.token, "person-temp-token")
XCTAssertEqual(response.accounts.map(\.businessUserId), [101, 201])
XCTAssertEqual(response.accounts.map(\.subtitle), ["scenic_operator", "store_admin"])
}
func testV9AuthResponseDecodesMissingAccountListsAsEmpty() throws {
let json = """
{
"code": 100000,
"msg": "success",
"data": {
"token": "only-token"
}
}
""".data(using: .utf8)!
let envelope = try JSONDecoder().decode(APIEnvelope<V9AuthResponse>.self, from: json)
let response = try XCTUnwrap(envelope.data)
XCTAssertEqual(response.token, "only-token")
XCTAssertTrue(response.scenicUsers.isEmpty)
XCTAssertTrue(response.storeUsers.isEmpty)
}
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",
"user_name": "scenic_operator",
"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().subtitle, "scenic_operator")
XCTAssertEqual(user.toAccountSwitchAccount().toSetUserRequest(), SetUserRequest(ssUserId: 101))
}
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().subtitle, "store_admin")
XCTAssertEqual(user.toAccountSwitchAccount().toSetUserRequest(), SetUserRequest(storeUserId: 201))
}
}