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, url, "*:*") if err != nil { return errors.WithStack(err) } if proxy == nil { return errors.Errorf("proxy should not be nil") } if proxy.ID == "" { return errors.Errorf("proxy.ID 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, url, "127.0.0.1:*", "localhost:*") if err != nil { return errors.WithStack(err) } foundProxy, err := repo.GetProxy(ctx, createdProxy.ID) if err != nil { return errors.WithStack(err) } if e, g := createdProxy.ID, foundProxy.ID; e != g { return errors.Errorf("foundProxy.ID: expected '%v', got '%v'", createdProxy.ID, foundProxy.ID) } 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, url, "127.0.0.1:*", "localhost:*") if err != nil { return errors.WithStack(err) } if err := repo.DeleteProxy(ctx, createdProxy.ID); err != nil { return errors.WithStack(err) } foundProxy, err := repo.GetProxy(ctx, createdProxy.ID) 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, 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.ID == createdProxy.ID { found = true break } } if !found { return errors.New("could not find created proxy in query results") } 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) } }