Skip to content

Latest commit

 

History

History
542 lines (397 loc) · 11.1 KB

File metadata and controls

542 lines (397 loc) · 11.1 KB

Xero CLI 測試指南

版本: 1.0
更新日期: 2026-02-28


測試環境準備

# 編譯
go build -o xero-cli.exe

# 或使用 go run
go run .

測試場景

1. Config Action 測試

1.1 查看配置(無配置)

go run . config

預期輸出:

No configuration found
Run 'xero-cli config --init' to initialize

1.2 JSON 輸出(無配置)

go run . config --json

預期輸出:

{}

1.3 初始化配置(需要手動輸入)

go run . config --init

交互流程:

Initialize Xero CLI Configuration
==================================

Step 1/5: Client ID
-------------------
Enter your Xero Client ID from Xero Developer Portal: 
> [輸入 client_id]

Step 2/5: Client Secret (Optional for PKCE)
-------------------------------------------
Enter your Xero Client Secret (leave empty for PKCE flow, recommended): 
> [輸入或直接 Enter 跳過]

Step 3/5: Redirect URI
----------------------
Enter Redirect URI [http://localhost:3456/callback]: 
> [Enter 使用預設]

Step 4/5: OAuth Scopes
----------------------
Enter OAuth Scopes [openid profile email accounting.transactions accounting.settings offline_access]: 
> [Enter 使用預設]

Step 5/5: Callback Server Port
------------------------------
Enter Callback Server Port (3000-9000) [3456]: 
> [Enter 使用預設]

Configuration Summary
---------------------
Client ID:     abc123-def456-ghi789
Client Secret: ****789
Redirect URI:  http://localhost:3456/callback
Scopes:        openid profile email accounting.transactions accounting.settings offline_access
Port:          3456

Save this configuration? [Y/n]: 
> [Enter 確認]

✓ Configuration saved successfully
  Config file: C:\Users\<user>\AppData\Roaming\xero-cli\config.json (encrypted)

Next step: Run 'xero-cli auth' to authenticate

檢查點:

  • ✅ 配置文件已創建並加密
  • ✅ 文件權限為 0600
  • ✅ 可以再次查看配置

1.4 查看配置(有配置)

go run . config

預期輸出:

Xero CLI Configuration
======================

Client ID:     abc123-def456-ghi789
Client Secret: ****789
Redirect URI:  http://localhost:3456/callback
Scopes:        openid profile email accounting.transactions accounting.settings offline_access
Port:          3456

Config File:   C:\Users\<user>\AppData\Roaming\xero-cli\config.json (encrypted)
Token File:    (not authenticated)

Commands:
  Use 'xero-cli config --init' to initialize configuration
  Use 'xero-cli config --update' to update configuration
  Use 'xero-cli config --reset' to reset configuration
  Use 'xero-cli auth' to authenticate with Xero

1.5 JSON 輸出(有配置)

go run . config --json

預期輸出:

{
  "client_id": "abc123-def456-ghi789",
  "redirect_uri": "http://localhost:3456/callback",
  "scopes": "openid profile email accounting.transactions accounting.settings offline_access",
  "port": "3456",
  "client_secret": "***masked***"
}

1.6 更新配置(需要手動輸入)

go run . config --update

交互流程:

Update Xero CLI Configuration
==============================

Current Configuration
---------------------
Client ID:     abc123-def456-ghi789
Client Secret: ****789
Redirect URI:  http://localhost:3456/callback
Scopes:        openid profile email accounting.transactions accounting.settings offline_access
Port:          3456

Press Enter to keep current value, or enter new value

Update Client ID [abc123-def456-ghi789]: 
> [Enter 保持不變]

Update Client Secret [****789] (leave empty to keep current): 
> [Enter 保持不變]

Update Redirect URI [http://localhost:3456/callback]: 
> http://localhost:4000/callback

Update Scopes [openid profile email ...]: 
> [Enter 保持不變]

Update Port [3456]: 
> 4000

Configuration will be updated:

Client ID:     abc123-def456-ghi789
Client Secret: ****789
Redirect URI:  http://localhost:4000/callback  (changed)
Scopes:        openid profile email accounting.transactions accounting.settings offline_access
Port:          4000  (changed)

Save changes? [Y/n]: 
> [Enter 確認]

✓ Configuration updated
  Token cleared (re-authentication required)
  Next: Run 'xero-cli auth' to authenticate with new configuration

檢查點:

  • ✅ 配置文件已更新
  • ✅ Token 已被清空(如果存在)

1.7 重置配置(需要確認)

go run . config --reset

交互流程:

Reset Xero CLI Configuration
=============================

WARNING: This will delete your configuration and clear saved tokens.

This action will:
  - Delete config file: C:\Users\<user>\AppData\Roaming\xero-cli\config.json
  - Delete token file: C:\Users\<user>\AppData\Roaming\xero-cli\tokens.json

Are you sure you want to continue? [y/N]: 
> y

✓ Configuration reset successfully

Next step: Run 'xero-cli config --init' to set up new configuration

檢查點:

  • ✅ Config 文件已刪除
  • ✅ Token 文件已刪除(如果存在)

1.8 取消重置

go run . config --reset

交互流程:

...
Are you sure you want to continue? [y/N]: 
> n

Reset cancelled

檢查點:

  • ✅ Config 文件仍然存在
  • ✅ Token 文件仍然存在

2. Auth Action 測試

2.1 Auth 無配置

go run . auth

預期錯誤:

Error: failed to load config: no configuration found
Run 'xero-cli config --init' or provide --client-id flag

2.2 Auth 已有有效 Token

go run . auth

預期輸出:

Already authenticated

Tenant: ABC Company Ltd
Expires: 2026-02-28T15:30:00Z
Scopes: openid profile email accounting.transactions accounting.settings offline_access

Use --force to re-authenticate or --switch to change tenant

2.3 Auth 強制重新認證

go run . auth --force

預期: 重新執行 OAuth flow

2.4 Auth 切換 Tenant

go run . auth --switch

預期:

  • 如果有多個 tenant,顯示選擇列表
  • 選擇後更新 Token 中的 TenantID

3. 邊界測試

3.1 無效 Port

go run . config --init
# 輸入 port: 2000

預期錯誤:

Invalid port: port must be between 3000-9000. Please try again.

3.2 空 Client ID

go run . config --init
# 直接 Enter

預期錯誤:

Error: client ID is required

3.3 取消配置保存

go run . config --init
# 完成輸入後,在確認時輸入 n

預期:

Configuration cancelled

4. 加密測試

4.1 檢查 Config 文件加密

# 查看 config 文件內容
cat ~/.config/xero-cli/config.json
# 或 Windows
type %APPDATA%\xero-cli\config.json

預期格式:

{
  "version": 1,
  "data": "base64_encrypted_data...",
  "nonce": "base64_nonce...",
  "platform": "windows"
}

4.2 檢查 Token 文件加密

# 查看 token 文件內容
cat ~/.config/xero-cli/tokens.json

預期格式: 同上,加密格式

4.3 文件權限

# Linux/macOS
ls -la ~/.config/xero-cli/

# Windows (PowerShell)
Get-Acl %APPDATA%\xero-cli\config.json | Format-List

預期: 僅所有者可讀寫(0600)


測試檢查清單

Config Action

  • config - 查看配置(無配置)
  • config --json - JSON 輸出(無配置)
  • config --init - 初始化配置
  • config - 查看配置(有配置)
  • config --json - JSON 輸出(有配置)
  • config --update - 更新配置
  • config --reset - 重置配置(確認)
  • config --reset - 重置配置(取消)

Auth Action

  • auth - 無配置
  • auth - 已有有效 token
  • auth --force - 強制重新認證
  • auth --switch - 切換 tenant
  • auth - 使用 CLI flags

邊界測試

  • 無效 Port
  • 空 Client ID
  • 取消配置保存

加密測試

  • Config 文件加密格式
  • Token 文件加密格式
  • 文件權限 0600

已知問題


待測試項目

  1. 多 Tenant 選擇流程(需要真實 Xero 賬號有多個 organization)
  2. PKCE flow vs Standard OAuth flow(需要有 client secret)
  3. Token Refresh 流程
  4. 跨機器測試(機器綁定驗證)

測試報告模板

### 測試結果

**測試日期**: YYYY-MM-DD  
**測試環境**: Windows 11 / Go 1.24.0  
**測試者**: [姓名]

#### 通過測試

- [ ] Config 查看
- [ ] Config 初始化
- [ ] Config 更新
- [ ] Config 重置
- [ ] Auth 基本流程

#### 失敗測試

- [ ] [測試名稱] - [失敗原因]

#### 發現問題

1. [問題描述]
2. [問題描述]

#### 建議改進

1. [建議內容]
2. [建議內容]

單元測試

運行測試命令

# 運行所有測試
go test ./...

# 詳細輸出
go test ./... -v

# 覆蓋率報告
go test ./... -cover

# 單一測試
go test -run ^TestGetPayment_Mock$ ./...
go test -run ^TestBuildOrderClause$ ./...
go test -run ^TestBuildCreditNoteWhereClause$ ./...

測試統計

測試類別 測試函數 子測試數量 狀態
API 測試
GetPayment TestGetPayment_Mock 1
GetPayment NotFound TestGetPayment_NotFound 1
GetPaymentsPaginated TestGetPaymentsPaginated_WithOrder 1
GetCreditNotes TestGetCreditNotes_Mock 1
GetCreditNote TestGetCreditNote_Mock 2
WHERE Clause 測試
normalizeWhereClause TestNormalizeWhereClause 10
buildInvoiceWhereClause TestBuildInvoiceWhereClause 5
buildContactWhereClause TestBuildContactWhereClause 7
buildAccountWhereClause TestBuildAccountWhereClause 6
buildPaymentWhereClause TestBuildPaymentWhereClause 8
buildCreditNoteWhereClause TestBuildCreditNoteWhereClause 7
排序測試
buildOrderClause TestBuildOrderClause 7
認證測試
OAuth2 TestGenerateCodeVerifier 1
OAuth2 TestGenerateCodeChallenge 1
Token TestTokenStoreFunctions 1
Config TestConfigValidation 1
API Client 測試
CallAPI TestAPIClient_CallAPI_Success 1
CallAPI Error TestAPIClient_CallAPI_Error 1
AutoRefresh TestAPIClient_CallWithAutoRefresh 3
總計 21 個測試函數 70+ 子測試 100% 通過

測試涵蓋範圍

測試類別 測試數量 說明
API 測試 10+ GetPayment, GetCreditNote, GetConnections, etc.
WHERE Clause 測試 50+ buildInvoiceWhereClause, buildContactWhereClause, etc.
排序測試 7+ buildOrderClause
認證測試 5+ OAuth2 PKCE, Token 管理
工具函數測試 10+ normalizeWhereClause, ConfigValidation
總計 70+

測試檔案清單

  • api_payments_test.go - Payments API 測試
  • api_creditnotes_test.go - CreditNotes API 測試
  • cmd_helpers_test.go - WHERE clause 和排序測試
  • api_test.go - API Client 基礎測試
  • auth_test.go - OAuth2 認證測試
  • config_test.go - 配置測試