2026-06-04 02:29:23 -07:00
|
|
|
@testable import ForgejoKit
|
2026-03-11 13:53:55 -07:00
|
|
|
import Foundation
|
|
|
|
|
import Testing
|
|
|
|
|
|
|
|
|
|
struct UserServiceTests {
|
2026-06-15 03:06:27 -07:00
|
|
|
@Test func createTokenPayloadEncodesCorrectKeys() throws {
|
2026-03-11 13:53:55 -07:00
|
|
|
let payload: [String: Any] = [
|
|
|
|
|
"name": "integration-test-token",
|
|
|
|
|
"scopes": ["read:user", "write:user"],
|
|
|
|
|
]
|
|
|
|
|
let data = try JSONSerialization.data(withJSONObject: payload)
|
2026-06-04 02:29:23 -07:00
|
|
|
let dict = try #require(JSONSerialization.jsonObject(with: data) as? [String: Any])
|
2026-03-11 13:53:55 -07:00
|
|
|
#expect(dict["name"] as? String == "integration-test-token")
|
|
|
|
|
#expect((dict["scopes"] as? [String])?.count == 2)
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-15 03:06:27 -07:00
|
|
|
@Test func createTokenURLConstruction() throws {
|
2026-03-11 13:53:55 -07:00
|
|
|
let client = ForgejoClient(serverURL: "https://forgejo.example.com", username: "admin", password: "pass")
|
|
|
|
|
let url = try client.makeURL(path: "/api/v1/users/\(ForgejoClient.encodedPathSegment("testuser"))/tokens")
|
|
|
|
|
#expect(url.absoluteString == "https://forgejo.example.com/api/v1/users/testuser/tokens")
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-15 03:06:27 -07:00
|
|
|
@Test func createTokenURLEncodesSpecialCharacters() throws {
|
2026-03-11 13:53:55 -07:00
|
|
|
let client = ForgejoClient(serverURL: "https://forgejo.example.com", username: "admin", password: "pass")
|
|
|
|
|
let encoded = ForgejoClient.encodedPathSegment("user name")
|
|
|
|
|
let url = try client.makeURL(path: "/api/v1/users/\(encoded)/tokens")
|
|
|
|
|
#expect(url.absoluteString.contains("user%20name"))
|
|
|
|
|
}
|
|
|
|
|
}
|