126 lines
3.2 KiB
Go
126 lines
3.2 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
appcrypto "netisjwt/internal/crypto"
|
|
"netisjwt/internal/statuslist"
|
|
"netisjwt/internal/storage"
|
|
)
|
|
|
|
type mockStore struct {
|
|
data map[string]*statuslist.StatusList
|
|
}
|
|
|
|
func newMockStore() *mockStore {
|
|
return &mockStore{data: make(map[string]*statuslist.StatusList)}
|
|
}
|
|
|
|
func (m *mockStore) Ustvari(_ context.Context, statusID string, list *statuslist.StatusList) error {
|
|
m.data[statusID] = mustCloneList(list)
|
|
return nil
|
|
}
|
|
|
|
func (m *mockStore) SeznamIDjev(_ context.Context) ([]string, error) {
|
|
out := make([]string, 0, len(m.data))
|
|
for id := range m.data {
|
|
out = append(out, id)
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func (m *mockStore) Dobi(_ context.Context, statusID string) (*statuslist.StatusList, error) {
|
|
list, ok := m.data[statusID]
|
|
if !ok {
|
|
return nil, storage.ErrNotFound
|
|
}
|
|
return mustCloneList(list), nil
|
|
}
|
|
|
|
func (m *mockStore) Posodobi(_ context.Context, statusID string, list *statuslist.StatusList) error {
|
|
if _, ok := m.data[statusID]; !ok {
|
|
return storage.ErrNotFound
|
|
}
|
|
m.data[statusID] = mustCloneList(list)
|
|
return nil
|
|
}
|
|
|
|
func mustCloneList(list *statuslist.StatusList) *statuslist.StatusList {
|
|
clone, err := statuslist.IzSurovihPodatkov(list.Bajti(), list.DolzinaBitov())
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return clone
|
|
}
|
|
|
|
func TestStatusFlow(t *testing.T) {
|
|
privatePEM, err := appcrypto.UstvariZasebniKljucECDSAP256PEM()
|
|
if err != nil {
|
|
t.Fatalf("key gen: %v", err)
|
|
}
|
|
|
|
store := newMockStore()
|
|
handler := NewObravnalnik(store, privatePEM)
|
|
router := NewUsmerjevalnik(handler)
|
|
|
|
statusID := createListAndReturnID(t, router)
|
|
createIndexAndSetTrue(t, router, statusID)
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/status/"+statusID+"/0", nil)
|
|
req.Host = "localhost:8000"
|
|
rec := httptest.NewRecorder()
|
|
router.ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("expected 200, got %d", rec.Code)
|
|
}
|
|
token := strings.TrimSpace(rec.Body.String())
|
|
if strings.Count(token, ".") != 2 {
|
|
t.Fatalf("expected compact JWS token")
|
|
}
|
|
}
|
|
|
|
func createListAndReturnID(t *testing.T, router http.Handler) string {
|
|
t.Helper()
|
|
req := httptest.NewRequest(http.MethodPost, "/api/status", nil)
|
|
rec := httptest.NewRecorder()
|
|
router.ServeHTTP(rec, req)
|
|
if rec.Code != http.StatusCreated {
|
|
t.Fatalf("expected 201, got %d", rec.Code)
|
|
}
|
|
|
|
var response struct {
|
|
StatusID string `json:"statusId"`
|
|
}
|
|
if err := json.Unmarshal(rec.Body.Bytes(), &response); err != nil {
|
|
t.Fatalf("invalid json: %v", err)
|
|
}
|
|
if response.StatusID == "" {
|
|
t.Fatalf("expected non-empty statusId")
|
|
}
|
|
return response.StatusID
|
|
}
|
|
|
|
func createIndexAndSetTrue(t *testing.T, router http.Handler, statusID string) {
|
|
t.Helper()
|
|
|
|
addReq := httptest.NewRequest(http.MethodPost, "/api/status/"+statusID, nil)
|
|
addRec := httptest.NewRecorder()
|
|
router.ServeHTTP(addRec, addReq)
|
|
if addRec.Code != http.StatusCreated {
|
|
t.Fatalf("expected 201, got %d", addRec.Code)
|
|
}
|
|
|
|
setReq := httptest.NewRequest(http.MethodPut, "/api/status/"+statusID+"/0", nil)
|
|
setRec := httptest.NewRecorder()
|
|
router.ServeHTTP(setRec, setReq)
|
|
if setRec.Code != http.StatusNoContent {
|
|
t.Fatalf("expected 204, got %d", setRec.Code)
|
|
}
|
|
}
|