88 lines
1.5 KiB
Go
88 lines
1.5 KiB
Go
package auth
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"local/dndex/storage"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func TestRegister(t *testing.T) {
|
|
g := storage.NewRateLimitedGraph()
|
|
namespace := uuid.New().String()
|
|
key := uuid.New().String()
|
|
r := httptest.NewRequest(
|
|
http.MethodPost,
|
|
"/",
|
|
strings.NewReader(
|
|
fmt.Sprintf(
|
|
"%s=%s&%s=%s",
|
|
UserKey,
|
|
namespace,
|
|
AuthKey,
|
|
key,
|
|
),
|
|
),
|
|
)
|
|
r.Header.Set("content-type", "application/x-www-form-urlencoded")
|
|
if err := r.ParseForm(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if v := readRequested(r, UserKey); v == "" {
|
|
t.Fatal(UserKey, v)
|
|
}
|
|
if v := readRequested(r, AuthKey); v == "" {
|
|
t.Fatal(AuthKey, v)
|
|
}
|
|
err := Register(g, r)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
one, err := g.Get(context.TODO(), namespace, UserKey)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if one.ID != UserKey {
|
|
t.Fatal(err)
|
|
}
|
|
if one.Title != key {
|
|
t.Fatal(one)
|
|
}
|
|
}
|
|
|
|
func TestRegisterEmpty(t *testing.T) {
|
|
g := storage.NewRateLimitedGraph()
|
|
key := uuid.New().String()
|
|
r := httptest.NewRequest(
|
|
http.MethodPost,
|
|
"/",
|
|
strings.NewReader(
|
|
fmt.Sprintf(
|
|
"%s=&%s=%s",
|
|
UserKey,
|
|
AuthKey,
|
|
key,
|
|
),
|
|
),
|
|
)
|
|
r.Header.Set("content-type", "application/x-www-form-urlencoded")
|
|
if err := r.ParseForm(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if v := readRequested(r, UserKey); v != "" {
|
|
t.Fatal(UserKey, v)
|
|
}
|
|
if v := readRequested(r, AuthKey); v == "" {
|
|
t.Fatal(AuthKey, v)
|
|
}
|
|
err := Register(g, r)
|
|
if err == nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|