70 lines
1.9 KiB
Go
70 lines
1.9 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
func TestStoreConfigWithAdminUIDKey(t *testing.T) {
|
|
const managerUIDKey = "MTIzNDU2Nzg5MGFiY2RlZg=="
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/internal/im-config/third-party/im" {
|
|
t.Errorf("unexpected path: %s", r.URL.Path)
|
|
return
|
|
}
|
|
if r.Header.Get(internalHeaderSignature) == "" {
|
|
t.Error("internal request must be signed")
|
|
return
|
|
}
|
|
_ = json.NewEncoder(w).Encode(map[string]any{
|
|
"code": 0,
|
|
"data": map[string]any{
|
|
"values": map[string]string{
|
|
"uid_encryption_key": managerUIDKey,
|
|
},
|
|
},
|
|
})
|
|
}))
|
|
defer server.Close()
|
|
|
|
client := &adminInternalClient{
|
|
config: adminInternalAPIConfig{BaseURL: server.URL, ServiceName: "im-server", Secret: "test-secret"},
|
|
http: server.Client(),
|
|
}
|
|
merged, ok, err := storeConfigWithAdminUIDKey(json.RawMessage(`{"uid_key":"bG9jYWwtZmFsbGJhY2s=","max_results":1024}`), client)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !ok {
|
|
t.Fatal("expected uid key to be loaded from manager")
|
|
}
|
|
var config struct {
|
|
UIDKey []byte `json:"uid_key"`
|
|
MaxResults int `json:"max_results"`
|
|
}
|
|
if err := json.Unmarshal(merged, &config); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if string(config.UIDKey) != "1234567890abcdef" {
|
|
t.Fatalf("unexpected uid key: %q", string(config.UIDKey))
|
|
}
|
|
if config.MaxResults != 1024 {
|
|
t.Fatalf("max_results should be preserved, got %d", config.MaxResults)
|
|
}
|
|
}
|
|
|
|
func TestStoreConfigWithAdminUIDKeyFallsBackWhenMissing(t *testing.T) {
|
|
merged, ok, err := storeConfigWithAdminUIDKey(json.RawMessage(`{"uid_key":"bG9jYWw="}`), nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if ok {
|
|
t.Fatal("nil admin client should not override uid key")
|
|
}
|
|
if string(merged) != `{"uid_key":"bG9jYWw="}` {
|
|
t.Fatalf("unexpected config: %s", string(merged))
|
|
}
|
|
}
|