From 6997a36f8757b45217eb0b131e316f411280a6b8 Mon Sep 17 00:00:00 2001 From: Clivern Date: Fri, 20 Aug 2021 21:28:13 +0200 Subject: [PATCH] chore: test cases --- core/component/tracing.go | 74 --------- core/controller/basic_auth_data_test.go | 193 +++++++++++++++++++++++- core/controller/me.go | 9 +- go.mod | 2 - go.sum | 8 - 5 files changed, 193 insertions(+), 93 deletions(-) delete mode 100644 core/component/tracing.go diff --git a/core/component/tracing.go b/core/component/tracing.go deleted file mode 100644 index 6e1aba9..0000000 --- a/core/component/tracing.go +++ /dev/null @@ -1,74 +0,0 @@ -// Copyright 2021 Clivern. All rights reserved. -// Use of this source code is governed by the MIT -// license that can be found in the LICENSE file. - -package component - -import ( - "fmt" - "io" - - opentracing "github.com/opentracing/opentracing-go" - "github.com/spf13/viper" - jaeger "github.com/uber/jaeger-client-go" - config "github.com/uber/jaeger-client-go/config" -) - -// Tracing type -type Tracing struct { - ServiceName string - Tracer opentracing.Tracer -} - -// NewTracingClient gets a new tracing client instance -func NewTracingClient(serviceName string) *Tracing { - return &Tracing{ - ServiceName: serviceName, - } -} - -// GetTracer gets tracer instance -func (t *Tracing) GetTracer() opentracing.Tracer { - return t.Tracer -} - -// IsEnabled checks if tracing is enabled -func (t *Tracing) IsEnabled() bool { - return viper.GetBool("app.component.tracing.status") -} - -// Init inits tracer -func (t *Tracing) Init() io.Closer { - var err error - var closer io.Closer - - cfg := &config.Configuration{ - ServiceName: t.ServiceName, - - // "const" sampler is a binary sampling strategy: - // 0 = never sample, - // 1 = always sample. - Sampler: &config.SamplerConfig{ - Type: "const", - Param: 1, - }, - - // Log the emitted spans to stdout. - Reporter: &config.ReporterConfig{ - QueueSize: viper.GetInt("app.component.tracing.queueSize"), - LogSpans: false, - CollectorEndpoint: viper.GetString("app.component.tracing.collectorEndpoint"), - }, - } - - t.Tracer, closer, err = cfg.NewTracer(config.Logger(jaeger.StdLogger)) - - if err != nil { - panic(fmt.Sprintf( - "Error: cannot init Jaeger: %v\n", - err, - )) - } - - return closer -} diff --git a/core/controller/basic_auth_data_test.go b/core/controller/basic_auth_data_test.go index a024844..aa8d165 100644 --- a/core/controller/basic_auth_data_test.go +++ b/core/controller/basic_auth_data_test.go @@ -8,6 +8,7 @@ import ( "fmt" "net/http" "net/http/httptest" + "strconv" "strings" "testing" @@ -34,7 +35,7 @@ func TestUnitBasicAuthDataEndpoints(t *testing.T) { defer database.Close() - database.CreateAuthMethod(&model.AuthMethod{ + result := database.CreateAuthMethod(&model.AuthMethod{ Name: "customers_public", Description: "Public API", Type: "any_authentication", @@ -53,31 +54,213 @@ func TestUnitBasicAuthDataEndpoints(t *testing.T) { g.Assert(err).Equal(nil) g.Assert(rec.Code).Equal(http.StatusBadRequest) - g.Assert(strings.Contains(rec.Body.String(), "message=BadReques")).Equal(true) + g.Assert(strings.Contains(rec.Body.String(), "message=BadRequest")).Equal(true) }) }) - g.Describe("#GetBasicAuthData", func() { + g.Describe("#CreateBasicAuthData.Failure", func() { g.It("It should satisfy all provided test cases", func() { + e := echo.New() + + item := &model.BasicAuthData{ + Name: "j.doe", + Username: "admin", + Password: "admin", + Meta: "", + } + + body, _ := item.ConvertToJSON() + + req := httptest.NewRequest(http.MethodPost, "/apigw/api/v1/auth/basic", strings.NewReader(body)) + rec := httptest.NewRecorder() + c := e.NewContext(req, rec) + c.SetPath("/apigw/api/v1/auth/basic") + + err := CreateBasicAuthData(c, &GlobalContext{Database: database}) + g.Assert(err).Equal(nil) + g.Assert(rec.Code).Equal(http.StatusBadRequest) + g.Assert(strings.Contains(rec.Body.String(), "message=BadRequest")).Equal(true) + }) + }) + + g.Describe("#CreateBasicAuthData.Success", func() { + g.It("It should satisfy all provided test cases", func() { + e := echo.New() + + item := &model.BasicAuthData{ + Name: "j.doe", + Username: "admin", + Password: "admin", + Meta: "", + AuthMethodID: result.ID, + } + + body, _ := item.ConvertToJSON() + + req := httptest.NewRequest(http.MethodPost, "/apigw/api/v1/auth/basic", strings.NewReader(body)) + rec := httptest.NewRecorder() + c := e.NewContext(req, rec) + c.SetPath("/apigw/api/v1/auth/basic") + + err := CreateBasicAuthData(c, &GlobalContext{Database: database}) + + g.Assert(err).Equal(nil) + g.Assert(rec.Code).Equal(http.StatusCreated) + }) + }) + + g.Describe("#GetBasicAuthData.Found", func() { + g.It("It should satisfy all provided test cases", func() { + item := database.CreateBasicAuthData(&model.BasicAuthData{ + Name: "j.doe", + Username: "admin", + Password: "admin", + Meta: "", + AuthMethodID: result.ID, + }) + + e := echo.New() + req := httptest.NewRequest(http.MethodPost, "/apigw/api/v1/auth/basic", nil) + rec := httptest.NewRecorder() + c := e.NewContext(req, rec) + c.SetParamNames("id") + c.SetParamValues(strconv.Itoa(item.ID)) + c.SetPath("/apigw/api/v1/auth/basic") + + err := GetBasicAuthData(c, &GlobalContext{Database: database}) + + g.Assert(err).Equal(nil) + g.Assert(rec.Code).Equal(http.StatusOK) + g.Assert(strings.Contains(rec.Body.String(), item.Name)).Equal(true) }) }) g.Describe("#GetBasicAuthItems", func() { g.It("It should satisfy all provided test cases", func() { + item := database.CreateBasicAuthData(&model.BasicAuthData{ + Name: "j.doe2", + Username: "admin", + Password: "admin", + Meta: "", + AuthMethodID: result.ID, + }) + + e := echo.New() + req := httptest.NewRequest(http.MethodPost, "/apigw/api/v1/auth/basic", nil) + rec := httptest.NewRecorder() + c := e.NewContext(req, rec) + c.SetPath("/apigw/api/v1/auth/basic") + + err := GetBasicAuthItems(c, &GlobalContext{Database: database}) + + g.Assert(err).Equal(nil) + g.Assert(rec.Code).Equal(http.StatusOK) + g.Assert(strings.Contains(rec.Body.String(), item.Name)).Equal(true) + }) + }) + + g.Describe("#DeleteBasicAuthData.Success", func() { + g.It("It should satisfy all provided test cases", func() { + item := database.CreateBasicAuthData(&model.BasicAuthData{ + Name: "j.doe", + Username: "admin", + Password: "admin", + Meta: "", + AuthMethodID: result.ID, + }) + e := echo.New() + req := httptest.NewRequest(http.MethodPost, "/apigw/api/v1/auth/basic", nil) + rec := httptest.NewRecorder() + c := e.NewContext(req, rec) + c.SetParamNames("id") + c.SetParamValues(strconv.Itoa(item.ID)) + c.SetPath("/apigw/api/v1/auth/basic") + + err := DeleteBasicAuthData(c, &GlobalContext{Database: database}) + + g.Assert(err).Equal(nil) + g.Assert(rec.Code).Equal(http.StatusNoContent) }) }) - g.Describe("#DeleteBasicAuthData", func() { + g.Describe("#DeleteBasicAuthData.NotFound", func() { g.It("It should satisfy all provided test cases", func() { + e := echo.New() + req := httptest.NewRequest(http.MethodPost, "/apigw/api/v1/auth/basic", nil) + rec := httptest.NewRecorder() + c := e.NewContext(req, rec) + c.SetParamNames("id") + c.SetParamValues("22222") + c.SetPath("/apigw/api/v1/auth/basic") + err := DeleteBasicAuthData(c, &GlobalContext{Database: database}) + + g.Assert(err).Equal(nil) + g.Assert(rec.Code).Equal(http.StatusNotFound) }) }) - g.Describe("#UpdateBasicAuthData", func() { + g.Describe("#UpdateBasicAuthData.Updated", func() { g.It("It should satisfy all provided test cases", func() { + item1 := database.CreateBasicAuthData(&model.BasicAuthData{ + Name: "j.doe01", + Username: "admin", + Password: "admin", + Meta: "", + AuthMethodID: result.ID, + }) + item2 := &model.BasicAuthData{ + Name: "j.doe02", + Username: "admin", + Password: "admin", + Meta: "", + AuthMethodID: result.ID, + } + + body, _ := item2.ConvertToJSON() + + e := echo.New() + req := httptest.NewRequest(http.MethodPost, "/apigw/api/v1/auth/basic", strings.NewReader(body)) + rec := httptest.NewRecorder() + c := e.NewContext(req, rec) + c.SetParamNames("id") + c.SetParamValues(strconv.Itoa(item1.ID)) + c.SetPath("/apigw/api/v1/auth/basic") + + err := UpdateBasicAuthData(c, &GlobalContext{Database: database}) + + g.Assert(err).Equal(nil) + g.Assert(rec.Code).Equal(http.StatusOK) + }) + }) + + g.Describe("#UpdateBasicAuthData.NotFound", func() { + g.It("It should satisfy all provided test cases", func() { + item := &model.BasicAuthData{ + Name: "j.doe02", + Username: "admin", + Password: "admin", + Meta: "", + AuthMethodID: result.ID, + } + + body, _ := item.ConvertToJSON() + + e := echo.New() + req := httptest.NewRequest(http.MethodPost, "/apigw/api/v1/auth/basic", strings.NewReader(body)) + rec := httptest.NewRecorder() + c := e.NewContext(req, rec) + c.SetParamNames("id") + c.SetParamValues("4444") + c.SetPath("/apigw/api/v1/auth/basic") + + err := UpdateBasicAuthData(c, &GlobalContext{Database: database}) + + g.Assert(err).Equal(nil) + g.Assert(rec.Code).Equal(http.StatusNotFound) }) }) } diff --git a/core/controller/me.go b/core/controller/me.go index cf5625b..0b9e9ac 100644 --- a/core/controller/me.go +++ b/core/controller/me.go @@ -13,11 +13,12 @@ import ( // Me controller func Me(c echo.Context, _ *GlobalContext) error { - log.WithFields(log.Fields{ - "status": "ok", - }).Info(`Create auth method`) + log.Info(`Get access status and permissions`) return c.JSON(http.StatusOK, map[string]interface{}{ - "status": "ok", + "id": "1", + "type": "ok", + "authMethod": "", + "services": "", }) } diff --git a/go.mod b/go.mod index debf605..83b567b 100644 --- a/go.mod +++ b/go.mod @@ -9,12 +9,10 @@ require ( github.com/jinzhu/gorm v1.9.16 github.com/labstack/echo-contrib v0.11.0 github.com/labstack/echo/v4 v4.6.1 - github.com/opentracing/opentracing-go v1.2.0 github.com/prometheus/client_golang v1.11.0 github.com/satori/go.uuid v1.2.0 github.com/sirupsen/logrus v1.8.1 github.com/spf13/cobra v1.2.1 github.com/spf13/viper v1.9.0 - github.com/uber/jaeger-client-go v2.29.1+incompatible gopkg.in/yaml.v2 v2.4.0 ) diff --git a/go.sum b/go.sum index 68951b5..db565af 100644 --- a/go.sum +++ b/go.sum @@ -45,7 +45,6 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9 dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= -github.com/HdrHistogram/hdrhistogram-go v1.1.0 h1:6dpdDPTRoo78HxAJ6T1HfMiKSnqhgRRqzCuPshRkQ7I= github.com/HdrHistogram/hdrhistogram-go v1.1.0/go.mod h1:yDgFjdqOqDEKOvasDdhWNXYg9BVp4O+o5f6V/ehm6Oo= github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= @@ -405,7 +404,6 @@ github.com/opentracing-contrib/go-observer v0.0.0-20170622124052-a52f23424492/go github.com/opentracing/basictracer-go v1.0.0/go.mod h1:QfBfYuafItcjQuMwinw9GhYKwFXS9KnPs5lxoYwgW74= github.com/opentracing/opentracing-go v1.0.2/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= -github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+1B0VhjKrZUs= github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc= github.com/openzipkin-contrib/zipkin-go-opentracing v0.4.5/go.mod h1:/wsWhb9smxSfWAKL3wpBW7V8scJMt8N8gnaMCS9E/cA= github.com/openzipkin/zipkin-go v0.1.6/go.mod h1:QgAqvLzwWbR/WpD4A3cGpPtJrZXNIiJc5AZX7/PBEpw= @@ -423,7 +421,6 @@ github.com/pierrec/lz4 v1.0.2-0.20190131084431-473cd7ce01a1/go.mod h1:3/3N9NVKO0 github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/profile v1.2.1/go.mod h1:hJw3o1OdXxsrSjjVksARp5W95eeEaEfptyVZyv6JUPA= github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI= @@ -503,7 +500,6 @@ github.com/streadway/amqp v0.0.0-20190404075320-75d898a42a94/go.mod h1:AZpEONHx3 github.com/streadway/amqp v0.0.0-20190827072141-edfb9018d271/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw= github.com/streadway/handy v0.0.0-20190108123426-d5acb3125c2a/go.mod h1:qNTQ5P5JnDBl6z3cMAg/SywNDC5ABu5ApDIw6lUbRmI= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= @@ -516,9 +512,6 @@ github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/uber/jaeger-client-go v2.25.0+incompatible/go.mod h1:WVhlPFC8FDjOFMMWRy2pZqQJSXxYSwNYOkTr/Z6d3Kk= -github.com/uber/jaeger-client-go v2.29.1+incompatible h1:R9ec3zO3sGpzs0abd43Y+fBZRJ9uiH6lXyR/+u6brW4= -github.com/uber/jaeger-client-go v2.29.1+incompatible/go.mod h1:WVhlPFC8FDjOFMMWRy2pZqQJSXxYSwNYOkTr/Z6d3Kk= -github.com/uber/jaeger-lib v2.4.0+incompatible h1:fY7QsGQWiCt8pajv4r7JEvmATdCVaWxXbjwyYwsNaLQ= github.com/uber/jaeger-lib v2.4.0+incompatible/go.mod h1:ComeNDZlWwrWnDv8aPp0Ba6+uUTzImX/AauajbLI56U= github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= @@ -550,7 +543,6 @@ go.opencensus.io v0.23.0/go.mod h1:XItmlyltB5F7CS4xOC1DcqMoFqwtC6OG2xF7mCv7P7E= go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI= go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= -go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= go.uber.org/multierr v1.3.0/go.mod h1:VgVr7evmIr6uPjLBxg28wmKNXyqE9akIJ5XnfpiKl+4=