221 lines
5.4 KiB
Go
221 lines
5.4 KiB
Go
package testsuite
|
|
|
|
import (
|
|
"context"
|
|
"net/url"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"forge.cadoles.com/cadoles/bouncer/internal/store"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
type proxyRepositoryTestCase struct {
|
|
Name string
|
|
Do func(repo store.ProxyRepository) error
|
|
}
|
|
|
|
var proxyRepositoryTestCases = []proxyRepositoryTestCase{
|
|
{
|
|
Name: "Create proxy",
|
|
Do: func(repo store.ProxyRepository) error {
|
|
ctx := context.Background()
|
|
|
|
url, err := url.Parse("http://example.com")
|
|
if err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
|
|
proxy, err := repo.CreateProxy(ctx, "create_proxy", url, "*:*")
|
|
if err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
|
|
if proxy == nil {
|
|
return errors.Errorf("proxy should not be nil")
|
|
}
|
|
|
|
if proxy.Name == "" {
|
|
return errors.Errorf("proxy.Name should not be empty")
|
|
}
|
|
|
|
if proxy.To == nil {
|
|
return errors.Errorf("proxy.To should not be nil")
|
|
}
|
|
|
|
if e, g := url.String(), proxy.To.String(); e != g {
|
|
return errors.Errorf("proxy.URL.String(): expected '%v', got '%v'", url.String(), proxy.To.String())
|
|
}
|
|
|
|
if proxy.CreatedAt.IsZero() {
|
|
return errors.Errorf("proxy.CreatedAt should not be zero value")
|
|
}
|
|
|
|
if proxy.UpdatedAt.IsZero() {
|
|
return errors.Errorf("proxy.UpdatedAt should not be zero value")
|
|
}
|
|
|
|
return nil
|
|
},
|
|
},
|
|
{
|
|
Name: "Create then get proxy",
|
|
Do: func(repo store.ProxyRepository) error {
|
|
ctx := context.Background()
|
|
|
|
url, err := url.Parse("http://example.com")
|
|
if err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
|
|
createdProxy, err := repo.CreateProxy(ctx, "create_then_get_proxy", url, "127.0.0.1:*", "localhost:*")
|
|
if err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
|
|
foundProxy, err := repo.GetProxy(ctx, createdProxy.Name)
|
|
if err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
|
|
if e, g := createdProxy.Name, foundProxy.Name; e != g {
|
|
return errors.Errorf("foundProxy.Name: expected '%v', got '%v'", createdProxy.Name, foundProxy.Name)
|
|
}
|
|
|
|
if e, g := createdProxy.From, foundProxy.From; !reflect.DeepEqual(e, g) {
|
|
return errors.Errorf("foundProxy.From: expected '%v', got '%v'", createdProxy.From, foundProxy.From)
|
|
}
|
|
|
|
if e, g := createdProxy.To.String(), foundProxy.To.String(); e != g {
|
|
return errors.Errorf("foundProxy.To: expected '%v', got '%v'", createdProxy.To, foundProxy.To)
|
|
}
|
|
|
|
if e, g := createdProxy.CreatedAt, foundProxy.CreatedAt; e != g {
|
|
return errors.Errorf("foundProxy.CreatedAt: expected '%v', got '%v'", createdProxy.CreatedAt, foundProxy.CreatedAt)
|
|
}
|
|
|
|
if e, g := createdProxy.UpdatedAt, foundProxy.UpdatedAt; e != g {
|
|
return errors.Errorf("foundProxy.UpdatedAt: expected '%v', got '%v'", createdProxy.UpdatedAt, foundProxy.UpdatedAt)
|
|
}
|
|
|
|
return nil
|
|
},
|
|
},
|
|
{
|
|
Name: "Create then delete proxy",
|
|
Do: func(repo store.ProxyRepository) error {
|
|
ctx := context.Background()
|
|
|
|
url, err := url.Parse("http://example.com")
|
|
if err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
|
|
createdProxy, err := repo.CreateProxy(ctx, "create_then_delete_proxy", url, "127.0.0.1:*", "localhost:*")
|
|
if err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
|
|
if err := repo.DeleteProxy(ctx, createdProxy.Name); err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
|
|
foundProxy, err := repo.GetProxy(ctx, createdProxy.Name)
|
|
if err == nil {
|
|
return errors.New("err should not be nil")
|
|
}
|
|
|
|
if !errors.Is(err, store.ErrNotFound) {
|
|
return errors.Errorf("err should be store.ErrNotFound, got '%+v'", err)
|
|
}
|
|
|
|
if foundProxy != nil {
|
|
return errors.Errorf("foundProxy should be nil, got '%v'", foundProxy)
|
|
}
|
|
|
|
return nil
|
|
},
|
|
},
|
|
{
|
|
Name: "Create then query",
|
|
Do: func(repo store.ProxyRepository) error {
|
|
ctx := context.Background()
|
|
|
|
url, err := url.Parse("http://example.com")
|
|
if err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
|
|
createdProxy, err := repo.CreateProxy(ctx, "create_then_query", url, "127.0.0.1:*", "localhost:*")
|
|
if err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
|
|
headers, err := repo.QueryProxy(ctx)
|
|
if err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
|
|
if len(headers) < 1 {
|
|
return errors.Errorf("len(headers): expected value > 1, got '%v'", len(headers))
|
|
}
|
|
|
|
found := false
|
|
|
|
for _, h := range headers {
|
|
if h.Name == createdProxy.Name {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
|
|
if !found {
|
|
return errors.New("could not find created proxy in query results")
|
|
}
|
|
|
|
return nil
|
|
},
|
|
},
|
|
{
|
|
Name: "Create already existing proxy",
|
|
Do: func(repo store.ProxyRepository) error {
|
|
ctx := context.Background()
|
|
|
|
url, err := url.Parse("http://example.com")
|
|
if err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
|
|
var name store.ProxyName = "create_already_existing_proxy"
|
|
|
|
_, err = repo.CreateProxy(ctx, name, url, "127.0.0.1:*", "localhost:*")
|
|
if err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
|
|
_, err = repo.CreateProxy(ctx, name, url, "127.0.0.1:*")
|
|
if err == nil {
|
|
return errors.New("err should not be nil")
|
|
}
|
|
|
|
if !errors.Is(err, store.ErrAlreadyExist) {
|
|
return errors.Errorf("err: expected store.ErrAlreadyExists, got '%+v'", err)
|
|
}
|
|
|
|
return nil
|
|
},
|
|
},
|
|
}
|
|
|
|
func TestProxyRepository(t *testing.T, repo store.ProxyRepository) {
|
|
for _, tc := range proxyRepositoryTestCases {
|
|
func(tc proxyRepositoryTestCase) {
|
|
t.Run(tc.Name, func(t *testing.T) {
|
|
if err := tc.Do(repo); err != nil {
|
|
t.Fatalf("%+v", errors.WithStack(err))
|
|
}
|
|
})
|
|
}(tc)
|
|
}
|
|
}
|