57 lines
1.9 KiB
Go
57 lines
1.9 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"git.cin.su/m/Assistant/contracts/identity"
|
|
)
|
|
|
|
func TestCreatorIdentityClientValidatesThroughCore(t *testing.T) {
|
|
core := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/v1/creator/sessions/current" || r.Header.Get("Authorization") != "Bearer bounded" {
|
|
t.Fatal("unexpected Identity request")
|
|
}
|
|
_ = json.NewEncoder(w).Encode(identity.CreatorSession{ActorID: "actor", PrincipalID: "principal", ExpiresAtUnix: 100})
|
|
}))
|
|
defer core.Close()
|
|
client, err := newCreatorIdentityClient(core.URL, core.Client())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
session, err := client.Session(context.Background(), "Bearer bounded")
|
|
if err != nil || session.PrincipalID != "principal" {
|
|
t.Fatalf("session = %+v, err = %v", session, err)
|
|
}
|
|
}
|
|
|
|
func TestCreatorIdentityClientDistinguishesDenialFromOutage(t *testing.T) {
|
|
denyingCore := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
http.Error(w, "denied", http.StatusUnauthorized)
|
|
}))
|
|
defer denyingCore.Close()
|
|
client, err := newCreatorIdentityClient(denyingCore.URL, denyingCore.Client())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := client.Session(context.Background(), "Bearer invalid"); !errors.Is(err, errCreatorSessionDenied) {
|
|
t.Fatalf("denial error = %v", err)
|
|
}
|
|
|
|
unavailableCore := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
http.Error(w, "unavailable", http.StatusServiceUnavailable)
|
|
}))
|
|
defer unavailableCore.Close()
|
|
client, err = newCreatorIdentityClient(unavailableCore.URL, unavailableCore.Client())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := client.Session(context.Background(), "Bearer bounded"); !errors.Is(err, errIdentityUnavailable) {
|
|
t.Fatalf("availability error = %v", err)
|
|
}
|
|
}
|