76 lines
3.1 KiB
Go
76 lines
3.1 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
|
|
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/dingtalkconfig"
|
|
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/secretstore"
|
|
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/settings"
|
|
)
|
|
|
|
type candidateRepoStub struct {
|
|
activated bool
|
|
ref string
|
|
}
|
|
|
|
func (r *candidateRepoStub) CandidateDingTalkConfig(context.Context, string) (string, string, error) {
|
|
return "app", r.ref, nil
|
|
}
|
|
func (r *candidateRepoStub) ActivateDingTalkCandidate(context.Context, string, string) error {
|
|
r.activated = true
|
|
return nil
|
|
}
|
|
|
|
type candidateVerifierStub struct{ ok bool }
|
|
|
|
func (v candidateVerifierStub) Verify(context.Context, string, string) dingtalkconfig.Result {
|
|
return dingtalkconfig.Result{OK: v.ok}
|
|
}
|
|
|
|
func TestCandidateActivatorRejectsOAuthWithoutActivation(t *testing.T) {
|
|
secrets, _ := secretstore.New(t.TempDir())
|
|
ref, _ := secrets.Put("dingtalk", "secret")
|
|
repo := &candidateRepoStub{ref: ref}
|
|
a := &dingTalkCandidateActivator{tenant: "t", repo: repo, secrets: secrets, verifier: candidateVerifierStub{}, bindings: map[string]settings.ResourceBinding{}, restart: func() {}}
|
|
if err := a.ActivateCandidate(context.Background()); err == nil {
|
|
t.Fatal("expected error")
|
|
}
|
|
if repo.activated {
|
|
t.Fatal("must not activate")
|
|
}
|
|
}
|
|
func TestCandidateActivatorValidatesSchemasThenActivates(t *testing.T) {
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("/token", func(w http.ResponseWriter, _ *http.Request) {
|
|
_, _ = w.Write([]byte(`{"accessToken":"token","expireIn":7200}`))
|
|
})
|
|
mux.HandleFunc("/v1.0/notable/bases/base/sheets/sheet/fields", func(w http.ResponseWriter, _ *http.Request) {
|
|
_, _ = w.Write([]byte(`{"value":[{"id":"q","name":"中文问题","type":"text"},{"id":"tm","name":"提问时间","type":"date"},{"id":"u","name":"提问人","type":"user"},{"id":"o","name":"原始问题","type":"text"},{"id":"a","name":"AI答案","type":"text"},{"id":"p","name":"流程触发人员信息","type":"text"},{"id":"l","name":"语言","type":"text"},{"id":"d","name":"部门","type":"text"},{"id":"r","name":"区域","type":"text"}]}`))
|
|
})
|
|
server := httptest.NewServer(mux)
|
|
defer server.Close()
|
|
secrets, _ := secretstore.New(t.TempDir())
|
|
ref, _ := secrets.Put("dingtalk", "secret")
|
|
repo := &candidateRepoStub{ref: ref}
|
|
restarted := make(chan struct{}, 1)
|
|
a := &dingTalkCandidateActivator{tenant: "t", repo: repo, secrets: secrets, verifier: candidateVerifierStub{ok: true}, bindings: map[string]settings.ResourceBinding{"question_table": {BaseID: "base", SheetID: "sheet", OperatorID: "u"}}, restart: func() { restarted <- struct{}{} }}
|
|
oldTokenURL, oldBaseURL := candidateTokenURL, candidateTableBaseURL
|
|
candidateTokenURL, candidateTableBaseURL = server.URL+"/token", server.URL
|
|
defer func() { candidateTokenURL, candidateTableBaseURL = oldTokenURL, oldBaseURL }()
|
|
if err := a.ActivateCandidate(context.Background()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !repo.activated {
|
|
t.Fatal("expected activation")
|
|
}
|
|
select {
|
|
case <-restarted:
|
|
case <-time.After(time.Second):
|
|
t.Fatal("restart not requested")
|
|
}
|
|
}
|