52 lines
1.7 KiB
Go
52 lines
1.7 KiB
Go
package oa
|
|
|
|
import (
|
|
"context"
|
|
"crypto/hmac"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"io"
|
|
"kra/internal/biz/business"
|
|
"kra/internal/config"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
func TestActivationGatewayPreservesIDsAndSignsOAPath(t *testing.T) {
|
|
key := "test-key"
|
|
calls := 0
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
calls++
|
|
body, _ := io.ReadAll(r.Body)
|
|
mac := hmac.New(sha256.New, []byte(key))
|
|
mac.Write([]byte(r.Method + "\n" + r.URL.EscapedPath() + "\n" + r.Header.Get("X-Business-Timestamp") + "\n"))
|
|
mac.Write(body)
|
|
if hex.EncodeToString(mac.Sum(nil)) != r.Header.Get("X-Business-Signature") || r.URL.Path != "/oa/internal/business/activation/apply" {
|
|
t.Error("invalid signature/path")
|
|
}
|
|
var values map[string]string
|
|
if err := json.Unmarshal(body, &values); err != nil {
|
|
t.Error(err)
|
|
}
|
|
if values["poolId"] != "9007199254740993" || values["busierId"] != "42" || values["userId"] != "7" {
|
|
t.Errorf("identity precision: %v", values)
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
io.WriteString(w, `{"code":0,"msg":"申请成功"}`)
|
|
}))
|
|
defer server.Close()
|
|
store := config.NewStore(&config.Config{BusinessOA: &config.BusinessOA{URL: server.URL + "/oa/", Key: key}})
|
|
if err := NewActivationGateway(store).Apply(context.Background(), business.ActivationApplication{UserID: 7, PoolID: 9007199254740993, BusierID: 42, SourceDB: "lx"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
store.Replace(&config.Config{})
|
|
if err := NewActivationGateway(store).Apply(context.Background(), business.ActivationApplication{}); err != business.ErrActivationSource {
|
|
t.Fatal("missing configuration must fail closed")
|
|
}
|
|
if calls != 1 {
|
|
t.Fatal("unexpected request")
|
|
}
|
|
}
|