135 lines
3.5 KiB
Go
135 lines
3.5 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"git.ipao.vip/rogee/go-sip/internal/configread"
|
|
)
|
|
|
|
func fixture(t *testing.T) (string, string) {
|
|
t.Helper()
|
|
dir := t.TempDir()
|
|
if err := os.Mkdir(filepath.Join(dir, "tasks"), 0700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
names := map[string]string{"sip.json": "config-read-sip.json", "providers.json": "config-read-providers.json", "quota.json": "config-read-quota.json", "tasks/1.json": "config-read-task-full.json"}
|
|
var id string
|
|
for target, example := range names {
|
|
raw, err := os.ReadFile(filepath.Join("../../contracts/schema/examples", example))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var fields map[string]json.RawMessage
|
|
if err = json.Unmarshal(raw, &fields); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = json.Unmarshal(fields["dispatcher_id"], &id); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if target == "tasks/1.json" {
|
|
fields["task_id"] = json.RawMessage(`"1"`)
|
|
raw, _ = json.Marshal(fields)
|
|
}
|
|
if err = os.WriteFile(filepath.Join(dir, target), raw, 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
return dir, id
|
|
}
|
|
|
|
type appTransport struct{ app *service }
|
|
|
|
func (tr appTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
return tr.app.app.Test(req)
|
|
}
|
|
|
|
func TestConfigClientReadsAllResources(t *testing.T) {
|
|
dir, id := fixture(t)
|
|
data, err := loadSnapshots(dir, id)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
svc := newService(data, "test-secret")
|
|
client, err := configread.NewClient("http://saas.test", id, "test-secret", &http.Client{Transport: appTransport{svc}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ctx := context.Background()
|
|
sip, err := client.ReadSIP(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
providers, err := client.ReadProviders(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
tasks, _, err := client.ReadAllTasks(ctx)
|
|
if err != nil || len(tasks) != 1 {
|
|
t.Fatalf("tasks=%v err=%v", tasks, err)
|
|
}
|
|
if _, err = client.ReadTask(ctx, "1", tasks[0].TenantID, sip, providers); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, path := range []string{"/internal/v1/dispatcher/task/missing", "/internal/v1/dispatcher/tenant/0/quota"} {
|
|
req, _ := http.NewRequest("GET", "http://saas.test"+path, nil)
|
|
req.Header.Set("X-DISPATCHER-id", id)
|
|
req.Header.Set("X-DISPATCHER-SECRET-KEY", "test-secret")
|
|
resp, err := svc.app.Test(req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
io.Copy(io.Discard, resp.Body)
|
|
resp.Body.Close()
|
|
if resp.StatusCode != 404 {
|
|
t.Fatalf("%s: %d", path, resp.StatusCode)
|
|
}
|
|
}
|
|
req, _ := http.NewRequest("GET", "http://saas.test/internal/v1/dispatcher/sip", nil)
|
|
resp, err := svc.app.Test(req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
resp.Body.Close()
|
|
if resp.StatusCode != 403 {
|
|
t.Fatal(resp.StatusCode)
|
|
}
|
|
}
|
|
|
|
func TestSnapshotErrorsAndReadOnly(t *testing.T) {
|
|
dir, id := fixture(t)
|
|
before := make(map[string][]byte)
|
|
filepath.WalkDir(dir, func(path string, entry os.DirEntry, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !entry.IsDir() {
|
|
before[path], err = os.ReadFile(path)
|
|
}
|
|
return err
|
|
})
|
|
if _, err := loadSnapshots(dir, id); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for path, want := range before {
|
|
got, err := os.ReadFile(path)
|
|
if err != nil || string(got) != string(want) {
|
|
t.Fatalf("input changed: %s", path)
|
|
}
|
|
}
|
|
if _, err := loadSnapshots(dir, "wrong-owner"); err == nil {
|
|
t.Fatal("accepted wrong owner")
|
|
}
|
|
if err := os.WriteFile(filepath.Join(dir, "sip.json"), []byte(`{"api_key":"must-not-leak"}`), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := loadSnapshots(dir, id); err == nil {
|
|
t.Fatal("accepted invalid snapshot")
|
|
}
|
|
}
|