-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_test.go
More file actions
107 lines (95 loc) · 3.33 KB
/
Copy pathclient_test.go
File metadata and controls
107 lines (95 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package hduhelp_test
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/hduhelp/hduhelp-neo-sdk-go"
"github.com/hduhelp/hduhelp-neo-sdk-go/service/academic"
)
// fakeGateway serves the tenant token endpoint plus one academic endpoint,
// recording the Authorization header seen on the academic call.
func fakeGateway(t *testing.T, gotAuth *string) *httptest.Server {
t.Helper()
mux := http.NewServeMux()
mux.HandleFunc("/hduhelp-neo/open-apis/auth/tenant-access-token/internal",
func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Errorf("tenant token method = %s, want POST", r.Method)
http.Error(w, "wrong method", http.StatusMethodNotAllowed)
return
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"code":0,"msg":"ok","data":{"tenantAccessToken":"tat-xyz","expire":7200}}`))
})
mux.HandleFunc("/hduhelp-neo/academic/schedule",
func(w http.ResponseWriter, r *http.Request) {
*gotAuth = r.Header.Get("Authorization")
w.Header().Set("X-Request-Id", "req-123")
w.Header().Set("Content-Type", "application/json")
resp := map[string]any{
"code": 0,
"msg": "ok",
"data": []map[string]any{{"courseName": "Compilers"}},
}
_ = json.NewEncoder(w).Encode(resp)
})
return httptest.NewServer(mux)
}
func TestClientInjectsTenantTokenAndParsesResponse(t *testing.T) {
var gotAuth string
srv := fakeGateway(t, &gotAuth)
defer srv.Close()
client := hduhelp.NewClient("cli_test", "secret", hduhelp.WithBaseURL(srv.URL))
req := academic.NewScheduleReqBuilder().
SchoolYear("2025-2026").
Semester(1).
Week(3).
Build()
resp, err := client.Academic.Schedule(context.Background(), req)
if err != nil {
t.Fatalf("Schedule: %v", err)
}
if !resp.Success() {
t.Fatalf("expected success, got code=%d msg=%q", resp.Code, resp.Msg)
}
if gotAuth != "Bearer tat-xyz" {
t.Fatalf("expected auto tenant token, got Authorization=%q", gotAuth)
}
if resp.RequestID() != "req-123" {
t.Fatalf("expected request id req-123, got %q", resp.RequestID())
}
if len(resp.Data) != 1 || resp.Data[0].CourseName == nil || *resp.Data[0].CourseName != "Compilers" {
t.Fatalf("unexpected typed data: %+v", resp.Data)
}
}
func TestPerRequestUserAccessTokenOverride(t *testing.T) {
var gotAuth string
srv := fakeGateway(t, &gotAuth)
defer srv.Close()
client := hduhelp.NewClient("cli_test", "secret", hduhelp.WithBaseURL(srv.URL))
req := academic.NewScheduleReqBuilder().SchoolYear("2025-2026").Build()
_, err := client.Academic.Schedule(context.Background(), req,
hduhelp.WithUserAccessToken("uat-override"))
if err != nil {
t.Fatalf("Schedule: %v", err)
}
if gotAuth != "Bearer uat-override" {
t.Fatalf("expected per-request UAT override, got %q", gotAuth)
}
}
func TestPATClientOption(t *testing.T) {
var gotAuth string
srv := fakeGateway(t, &gotAuth)
defer srv.Close()
// A client-level PAT authenticates every call without touching the token endpoint.
client := hduhelp.NewClient("", "", hduhelp.WithBaseURL(srv.URL), hduhelp.WithPAT("hduhelp_pat_abc"))
req := academic.NewScheduleReqBuilder().Build()
if _, err := client.Academic.Schedule(context.Background(), req); err != nil {
t.Fatalf("Schedule: %v", err)
}
if gotAuth != "Bearer hduhelp_pat_abc" {
t.Fatalf("expected PAT bearer, got %q", gotAuth)
}
}