From 0398fec07c4a887a54be946c4fb99b1a08c12935 Mon Sep 17 00:00:00 2001 From: Taufan Adhitya Date: Fri, 27 Sep 2024 18:08:32 +0700 Subject: [PATCH 01/13] feat: add request and bind method --- pkg/db/postgrest_request.go | 68 ++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/pkg/db/postgrest_request.go b/pkg/db/postgrest_request.go index bfc57739..b6c3fbb2 100644 --- a/pkg/db/postgrest_request.go +++ b/pkg/db/postgrest_request.go @@ -1,6 +1,7 @@ package db import ( + "encoding/json" "flag" "fmt" "strings" @@ -45,7 +46,6 @@ func PostgrestRequest(ctx raiden.Context, method string, url string, payload []b bearerToken := string(ctx.RequestContext().Request.Header.Peek("Authorization")) if bearerToken != "" && strings.HasPrefix(bearerToken, "Bearer ") { - bearerToken = strings.TrimSpace(strings.TrimPrefix(bearerToken, "Bearer ")) req.Header.Set("Authorization", bearerToken) } @@ -69,6 +69,72 @@ func PostgrestRequest(ctx raiden.Context, method string, url string, payload []b return body, res, nil } +func PostgrestRequestBind(ctx raiden.Context, method string, url string, payload []byte, headers map[string]string, result interface{}) (*fasthttp.Response, error) { + + if !isAllowedMethod(method) { + return nil, nil, fmt.Errorf("method %s is not allowed", method) + } + + client := &fasthttp.Client{} + + req := fasthttp.AcquireRequest() + defer fasthttp.ReleaseRequest(req) + + if !strings.HasPrefix(url, "http://") && !strings.HasPrefix(url, "https://") { + var baseUrl string + if flag.Lookup("test.v") != nil { + baseUrl = "https://api.supabase.co" + } else { + baseUrl = getConfig().SupabasePublicUrl + } + + if strings.HasPrefix(url, "/") { + url = fmt.Sprintf("%s/rest/v1%s", baseUrl, url) + } else { + url = fmt.Sprintf("%s/rest/v1/%s", baseUrl, url) + } + } + + req.SetRequestURI(url) + req.Header.SetMethod(method) + + apikey := string(ctx.RequestContext().Request.Header.Peek("apikey")) + if apikey != "" { + req.Header.Set("apikey", apikey) + } + + bearerToken := string(ctx.RequestContext().Request.Header.Peek("Authorization")) + if bearerToken != "" && strings.HasPrefix(bearerToken, "Bearer ") { + bearerToken = strings.TrimSpace(strings.TrimPrefix(bearerToken, "Bearer ")) + req.Header.Set("Authorization", bearerToken) + } + + for key, value := range headers { + req.Header.Set(key, value) + } + + if payload != nil { + req.SetBody(payload) + } + + res := fasthttp.AcquireResponse() + defer fasthttp.ReleaseResponse(res) + + if err := client.Do(req, res); err != nil { + return nil, res, err + } + + body := res.Body() + + if result != nil { + if err := json.Unmarshal(body, result); err != nil { + return res, fmt.Errorf("failed to unmarshal response body: %w", err) + } + } + + return res, nil +} + func isAllowedMethod(method string) bool { methods := []string{ fasthttp.MethodGet, From de93e879c7004d419e5978de4e61b87835d737d1 Mon Sep 17 00:00:00 2001 From: Taufan Adhitya Date: Fri, 27 Sep 2024 18:11:53 +0700 Subject: [PATCH 02/13] feat: fix signature --- pkg/db/postgrest_request.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/db/postgrest_request.go b/pkg/db/postgrest_request.go index b6c3fbb2..235c5d70 100644 --- a/pkg/db/postgrest_request.go +++ b/pkg/db/postgrest_request.go @@ -121,7 +121,7 @@ func PostgrestRequestBind(ctx raiden.Context, method string, url string, payload defer fasthttp.ReleaseResponse(res) if err := client.Do(req, res); err != nil { - return nil, res, err + return res, err } body := res.Body() From 78ccf346b461300bb56b200dc999e29e1c84016c Mon Sep 17 00:00:00 2001 From: Taufan Adhitya Date: Fri, 27 Sep 2024 18:13:33 +0700 Subject: [PATCH 03/13] feat: fix signature --- pkg/db/postgrest_request.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/db/postgrest_request.go b/pkg/db/postgrest_request.go index 235c5d70..b772134d 100644 --- a/pkg/db/postgrest_request.go +++ b/pkg/db/postgrest_request.go @@ -72,7 +72,7 @@ func PostgrestRequest(ctx raiden.Context, method string, url string, payload []b func PostgrestRequestBind(ctx raiden.Context, method string, url string, payload []byte, headers map[string]string, result interface{}) (*fasthttp.Response, error) { if !isAllowedMethod(method) { - return nil, nil, fmt.Errorf("method %s is not allowed", method) + return nil, fmt.Errorf("method %s is not allowed", method) } client := &fasthttp.Client{} From 96c22ccd713bd423e4eeca387a4df9c800e18b02 Mon Sep 17 00:00:00 2001 From: Taufan Adhitya Date: Fri, 27 Sep 2024 18:39:46 +0700 Subject: [PATCH 04/13] feat: refactor to model binding --- pkg/db/aggregate.go | 3 +- pkg/db/db.go | 23 +++++++---- pkg/db/delete.go | 9 +++-- pkg/db/insert.go | 8 ++-- pkg/db/postgrest_request.go | 80 ++++++------------------------------- pkg/db/update.go | 8 ++-- pkg/db/upsert.go | 9 +++-- 7 files changed, 47 insertions(+), 93 deletions(-) diff --git a/pkg/db/aggregate.go b/pkg/db/aggregate.go index 6718046f..2335ef72 100644 --- a/pkg/db/aggregate.go +++ b/pkg/db/aggregate.go @@ -37,7 +37,8 @@ func (q *Query) Count(opts ...CountOptions) (int, error) { headers["Prefer"] = "count=" + countVal headers["Range-Unit"] = "items" - _, resp, err := PostgrestRequest(q.Context, fasthttp.MethodHead, url, nil, headers) + var a interface{} + resp, err := PostgrestRequestBind(q.Context, fasthttp.MethodHead, url, nil, headers, q.ByPass, &a) if err != nil { return 0, err } diff --git a/pkg/db/db.go b/pkg/db/db.go index e728df17..72cef409 100644 --- a/pkg/db/db.go +++ b/pkg/db/db.go @@ -21,6 +21,7 @@ type Query struct { LimitValue int OffsetValue int Errors []error + ByPass bool } type ModelBase struct { @@ -34,6 +35,7 @@ func (q *Query) HasError() bool { func NewQuery(ctx raiden.Context) *Query { return &Query{ Context: ctx, + bypass: false, } } @@ -47,6 +49,11 @@ func (q *Query) From(m interface{}) *Query { return q } +func (q *Query) ByPass() *Query { + q.bypass = true + return q +} + func GetTable(m interface{}) string { t := reflect.TypeOf(m) @@ -69,7 +76,7 @@ func (m *ModelBase) Execute() (model *ModelBase) { return m } -func (q Query) Get() ([]byte, error) { +func (q Query) Get(collection interface{}) error { url := q.GetUrl() @@ -77,27 +84,27 @@ func (q Query) Get() ([]byte, error) { headers["Content-Type"] = "application/json" headers["Prefer"] = "return=representation" - resp, _, err := PostgrestRequest(q.Context, fasthttp.MethodGet, url, nil, headers) + _, err := PostgrestRequestBind(q.Context, fasthttp.MethodGet, url, nil, headers, q.ByPass, collection) if err != nil { - return nil, err + return err } - return resp, nil + return nil } -func (q Query) Single() ([]byte, error) { +func (q Query) Single(model interface{}) error { url := q.Limit(1).GetUrl() headers := make(map[string]string) headers["Accept"] = "application/vnd.pgrst.object+json" - res, _, err := PostgrestRequest(q.Context, "GET", url, nil, headers) + _, err := PostgrestRequestBind(q.Context, "GET", url, nil, headers, q.ByPass, model) if err != nil { - return nil, err + return err } - return res, nil + return nil } func (q Query) GetUrl() string { diff --git a/pkg/db/delete.go b/pkg/db/delete.go index be198aa7..1b65b48c 100644 --- a/pkg/db/delete.go +++ b/pkg/db/delete.go @@ -4,17 +4,18 @@ import ( "github.com/valyala/fasthttp" ) -func (q *Query) Delete() ([]byte, error) { +func (q *Query) Delete() error { url := q.GetUrl() headers := make(map[string]string) headers["Content-Type"] = "application/json" headers["Prefer"] = "return=representation" - resp, _, err := PostgrestRequest(q.Context, fasthttp.MethodDelete, url, nil, headers) + var a interface{} + _, err := PostgrestRequestBind(q.Context, fasthttp.MethodDelete, url, nil, headers, q.ByPass, &a) if err != nil { - return nil, err + return err } - return resp, nil + return nil } diff --git a/pkg/db/insert.go b/pkg/db/insert.go index 24a29671..5b394e53 100644 --- a/pkg/db/insert.go +++ b/pkg/db/insert.go @@ -6,7 +6,7 @@ import ( "github.com/valyala/fasthttp" ) -func (q *Query) Insert(payload interface{}) ([]byte, error) { +func (q *Query) Insert(payload interface{}, model interface{}) error { jsonData, err := json.Marshal(payload) if err != nil { return nil, err @@ -18,10 +18,10 @@ func (q *Query) Insert(payload interface{}) ([]byte, error) { headers["Content-Type"] = "application/json" headers["Prefer"] = "return=representation" - body, _, err := PostgrestRequest(q.Context, fasthttp.MethodPost, url, jsonData, headers) + _, err := PostgrestRequestBind(q.Context, fasthttp.MethodPost, url, jsonData, headers, q.ByPass, model) if err != nil { - return nil, err + return err } - return body, nil + return nil } diff --git a/pkg/db/postgrest_request.go b/pkg/db/postgrest_request.go index b772134d..27b68c09 100644 --- a/pkg/db/postgrest_request.go +++ b/pkg/db/postgrest_request.go @@ -10,10 +10,10 @@ import ( "github.com/valyala/fasthttp" ) -func PostgrestRequest(ctx raiden.Context, method string, url string, payload []byte, headers map[string]string) ([]byte, *fasthttp.Response, error) { +func PostgrestRequestBind(ctx raiden.Context, method string, url string, payload []byte, headers map[string]string, bypass bool, result interface{}) (*fasthttp.Response, error) { if !isAllowedMethod(method) { - return nil, nil, fmt.Errorf("method %s is not allowed", method) + return nil, fmt.Errorf("method %s is not allowed", method) } client := &fasthttp.Client{} @@ -39,76 +39,20 @@ func PostgrestRequest(ctx raiden.Context, method string, url string, payload []b req.SetRequestURI(url) req.Header.SetMethod(method) - apikey := string(ctx.RequestContext().Request.Header.Peek("apikey")) - if apikey != "" { - req.Header.Set("apikey", apikey) - } - - bearerToken := string(ctx.RequestContext().Request.Header.Peek("Authorization")) - if bearerToken != "" && strings.HasPrefix(bearerToken, "Bearer ") { - req.Header.Set("Authorization", bearerToken) - } - - for key, value := range headers { - req.Header.Set(key, value) - } - - if payload != nil { - req.SetBody(payload) - } - - res := fasthttp.AcquireResponse() - defer fasthttp.ReleaseResponse(res) - - if err := client.Do(req, res); err != nil { - return nil, res, err - } - - body := res.Body() - - return body, res, nil -} - -func PostgrestRequestBind(ctx raiden.Context, method string, url string, payload []byte, headers map[string]string, result interface{}) (*fasthttp.Response, error) { - - if !isAllowedMethod(method) { - return nil, fmt.Errorf("method %s is not allowed", method) - } - - client := &fasthttp.Client{} - - req := fasthttp.AcquireRequest() - defer fasthttp.ReleaseRequest(req) - - if !strings.HasPrefix(url, "http://") && !strings.HasPrefix(url, "https://") { - var baseUrl string - if flag.Lookup("test.v") != nil { - baseUrl = "https://api.supabase.co" - } else { - baseUrl = getConfig().SupabasePublicUrl + if bypass { + req.Header.Set("apikey", getConfig().ServiceKey) + req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", getConfig().ServiceKey)) + } else { + apikey := string(ctx.RequestContext().Request.Header.Peek("apikey")) + if apikey != "" { + req.Header.Set("apikey", apikey) } - - if strings.HasPrefix(url, "/") { - url = fmt.Sprintf("%s/rest/v1%s", baseUrl, url) - } else { - url = fmt.Sprintf("%s/rest/v1/%s", baseUrl, url) + bearerToken := string(ctx.RequestContext().Request.Header.Peek("Authorization")) + if bearerToken != "" && strings.HasPrefix(bearerToken, "Bearer ") { + req.Header.Set("Authorization", bearerToken) } } - req.SetRequestURI(url) - req.Header.SetMethod(method) - - apikey := string(ctx.RequestContext().Request.Header.Peek("apikey")) - if apikey != "" { - req.Header.Set("apikey", apikey) - } - - bearerToken := string(ctx.RequestContext().Request.Header.Peek("Authorization")) - if bearerToken != "" && strings.HasPrefix(bearerToken, "Bearer ") { - bearerToken = strings.TrimSpace(strings.TrimPrefix(bearerToken, "Bearer ")) - req.Header.Set("Authorization", bearerToken) - } - for key, value := range headers { req.Header.Set(key, value) } diff --git a/pkg/db/update.go b/pkg/db/update.go index 89e57cd3..41fed101 100644 --- a/pkg/db/update.go +++ b/pkg/db/update.go @@ -8,7 +8,7 @@ import ( "github.com/valyala/fasthttp" ) -func (q *Query) Update(p interface{}) ([]byte, error) { +func (q *Query) Update(p interface{}, model interface{}) error { jsonData, err := json.Marshal(p) if err != nil { return nil, err @@ -29,10 +29,10 @@ func (q *Query) Update(p interface{}) ([]byte, error) { headers["Content-Type"] = "application/json" headers["Prefer"] = "return=representation" - body, _, err := PostgrestRequest(q.Context, fasthttp.MethodPatch, url, jsonData, headers) + _, err := PostgrestRequestBind(q.Context, fasthttp.MethodPatch, url, jsonData, headers, q.ByPass, model) if err != nil { - return nil, err + return err } - return body, nil + return nil } diff --git a/pkg/db/upsert.go b/pkg/db/upsert.go index 85107f88..6ab043ee 100644 --- a/pkg/db/upsert.go +++ b/pkg/db/upsert.go @@ -15,7 +15,7 @@ const ( IgnoreDuplicates = "ignore-duplicates" ) -func (q *Query) Upsert(payload []interface{}, opt UpsertOptions) ([]byte, error) { +func (q *Query) Upsert(payload []interface{}, opt UpsertOptions) error { jsonData, err := json.Marshal(payload) if err != nil { return nil, err @@ -27,10 +27,11 @@ func (q *Query) Upsert(payload []interface{}, opt UpsertOptions) ([]byte, error) headers["Content-Type"] = "application/json" headers["Prefer"] = "resolution=" + opt.OnConflict - resp, _, err := PostgrestRequest(q.Context, fasthttp.MethodPost, url, jsonData, headers) + var a interface{} + _, err := PostgrestRequestBind(q.Context, fasthttp.MethodPost, url, jsonData, headers, q.ByPass, &a) if err != nil { - return nil, err + return err } - return resp, nil + return nil } From 15060ff4b97e817998b8a21e553b7e26ad311c2a Mon Sep 17 00:00:00 2001 From: Taufan Adhitya Date: Fri, 27 Sep 2024 18:43:01 +0700 Subject: [PATCH 05/13] feat: fix AsSystem flag --- pkg/db/db.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/db/db.go b/pkg/db/db.go index 72cef409..72f69b86 100644 --- a/pkg/db/db.go +++ b/pkg/db/db.go @@ -35,7 +35,7 @@ func (q *Query) HasError() bool { func NewQuery(ctx raiden.Context) *Query { return &Query{ Context: ctx, - bypass: false, + ByPass: false, } } @@ -49,8 +49,8 @@ func (q *Query) From(m interface{}) *Query { return q } -func (q *Query) ByPass() *Query { - q.bypass = true +func (q *Query) AsSystem() *Query { + q.ByPass = true return q } From 2aaab4fd919ad863592c0548f4236860eda87097 Mon Sep 17 00:00:00 2001 From: Taufan Adhitya Date: Fri, 27 Sep 2024 18:48:06 +0700 Subject: [PATCH 06/13] feat: refactor signatures --- pkg/db/db_test.go | 6 ++++-- pkg/db/insert.go | 8 ++++---- pkg/db/update.go | 8 ++++---- pkg/db/upsert.go | 8 ++++---- 4 files changed, 16 insertions(+), 14 deletions(-) diff --git a/pkg/db/db_test.go b/pkg/db/db_test.go index 9b98e01d..18ca12b2 100644 --- a/pkg/db/db_test.go +++ b/pkg/db/db_test.go @@ -112,13 +112,15 @@ func TestGetTable(t *testing.T) { } func TestSingle(t *testing.T) { - _, err := NewQuery(&mockRaidenContext).Model(articleMockModel).Single() + var articleMockModel = ArticleMockModel{} + _, err := NewQuery(&mockRaidenContext).Model(articleMockModel).Single(&articleMockModel) assert.NoError(t, err) } func TestGet(t *testing.T) { - _, err := NewQuery(&mockRaidenContext).Model(articleMockModel).Get() + var collection []ArticleMockModel + _, err := NewQuery(&mockRaidenContext).Model(articleMockModel).Get(&collection) assert.NoError(t, err) } diff --git a/pkg/db/insert.go b/pkg/db/insert.go index 5b394e53..2843afe8 100644 --- a/pkg/db/insert.go +++ b/pkg/db/insert.go @@ -9,7 +9,7 @@ import ( func (q *Query) Insert(payload interface{}, model interface{}) error { jsonData, err := json.Marshal(payload) if err != nil { - return nil, err + return err } url := q.GetUrl() @@ -18,9 +18,9 @@ func (q *Query) Insert(payload interface{}, model interface{}) error { headers["Content-Type"] = "application/json" headers["Prefer"] = "return=representation" - _, err := PostgrestRequestBind(q.Context, fasthttp.MethodPost, url, jsonData, headers, q.ByPass, model) - if err != nil { - return err + _, err0 := PostgrestRequestBind(q.Context, fasthttp.MethodPost, url, jsonData, headers, q.ByPass, model) + if err0 != nil { + return err0 } return nil diff --git a/pkg/db/update.go b/pkg/db/update.go index 41fed101..e0f05452 100644 --- a/pkg/db/update.go +++ b/pkg/db/update.go @@ -11,7 +11,7 @@ import ( func (q *Query) Update(p interface{}, model interface{}) error { jsonData, err := json.Marshal(p) if err != nil { - return nil, err + return err } url := q.GetUrl() @@ -29,9 +29,9 @@ func (q *Query) Update(p interface{}, model interface{}) error { headers["Content-Type"] = "application/json" headers["Prefer"] = "return=representation" - _, err := PostgrestRequestBind(q.Context, fasthttp.MethodPatch, url, jsonData, headers, q.ByPass, model) - if err != nil { - return err + _, err0 := PostgrestRequestBind(q.Context, fasthttp.MethodPatch, url, jsonData, headers, q.ByPass, model) + if err0 != nil { + return err0 } return nil diff --git a/pkg/db/upsert.go b/pkg/db/upsert.go index 6ab043ee..d4e37f1b 100644 --- a/pkg/db/upsert.go +++ b/pkg/db/upsert.go @@ -18,7 +18,7 @@ const ( func (q *Query) Upsert(payload []interface{}, opt UpsertOptions) error { jsonData, err := json.Marshal(payload) if err != nil { - return nil, err + return err } url := q.GetUrl() @@ -28,9 +28,9 @@ func (q *Query) Upsert(payload []interface{}, opt UpsertOptions) error { headers["Prefer"] = "resolution=" + opt.OnConflict var a interface{} - _, err := PostgrestRequestBind(q.Context, fasthttp.MethodPost, url, jsonData, headers, q.ByPass, &a) - if err != nil { - return err + _, err0 := PostgrestRequestBind(q.Context, fasthttp.MethodPost, url, jsonData, headers, q.ByPass, &a) + if err0 != nil { + return err0 } return nil From b0d43ab0f484161a6fbc6f26120d0c36a74c1fdc Mon Sep 17 00:00:00 2001 From: Taufan Adhitya Date: Fri, 27 Sep 2024 18:51:37 +0700 Subject: [PATCH 07/13] fix: insert, update delete tests --- pkg/db/db_test.go | 4 ++-- pkg/db/delete_test.go | 2 +- pkg/db/insert_test.go | 4 ++-- pkg/db/update_test.go | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/pkg/db/db_test.go b/pkg/db/db_test.go index 18ca12b2..4e754073 100644 --- a/pkg/db/db_test.go +++ b/pkg/db/db_test.go @@ -113,14 +113,14 @@ func TestGetTable(t *testing.T) { func TestSingle(t *testing.T) { var articleMockModel = ArticleMockModel{} - _, err := NewQuery(&mockRaidenContext).Model(articleMockModel).Single(&articleMockModel) + err := NewQuery(&mockRaidenContext).Model(articleMockModel).Single(&articleMockModel) assert.NoError(t, err) } func TestGet(t *testing.T) { var collection []ArticleMockModel - _, err := NewQuery(&mockRaidenContext).Model(articleMockModel).Get(&collection) + err := NewQuery(&mockRaidenContext).Model(articleMockModel).Get(&collection) assert.NoError(t, err) } diff --git a/pkg/db/delete_test.go b/pkg/db/delete_test.go index 55c8833c..a79f9584 100644 --- a/pkg/db/delete_test.go +++ b/pkg/db/delete_test.go @@ -7,7 +7,7 @@ import ( ) func TestDelete(t *testing.T) { - _, err := NewQuery(&mockRaidenContext).Model(articleMockModel).Delete() + err := NewQuery(&mockRaidenContext).Model(articleMockModel).Delete() assert.NoError(t, err) } diff --git a/pkg/db/insert_test.go b/pkg/db/insert_test.go index 895e9acf..840c9c64 100644 --- a/pkg/db/insert_test.go +++ b/pkg/db/insert_test.go @@ -16,9 +16,9 @@ func TestInsert(t *testing.T) { CreatedAt: time.Now(), } - _, err := NewQuery(&mockRaidenContext). + err := NewQuery(&mockRaidenContext). Model(articleMockModel). - Insert(article) + Insert(article, &articleMockModel) assert.NoError(t, err) } diff --git a/pkg/db/update_test.go b/pkg/db/update_test.go index 74ac9eed..e4ed2b70 100644 --- a/pkg/db/update_test.go +++ b/pkg/db/update_test.go @@ -14,10 +14,10 @@ func TestUpdate(t *testing.T) { CreatedAt: time.Now(), } - _, err := NewQuery(&mockRaidenContext). + err := NewQuery(&mockRaidenContext). Model(articleMockModel). Eq("id", 1). - Update(article) + Update(article, &articleMockModel) assert.NoError(t, err) } From 0e583c6e0b916c67e9bdf61717bc3125e9686029 Mon Sep 17 00:00:00 2001 From: Taufan Adhitya Date: Fri, 27 Sep 2024 18:52:42 +0700 Subject: [PATCH 08/13] fix: upsert test --- pkg/db/upsert_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/db/upsert_test.go b/pkg/db/upsert_test.go index 4f156a77..31dd54c0 100644 --- a/pkg/db/upsert_test.go +++ b/pkg/db/upsert_test.go @@ -36,7 +36,7 @@ func TestUpsert(t *testing.T) { OnConflict: MergeDuplicates, } - _, err := NewQuery(&mockRaidenContext). + err := NewQuery(&mockRaidenContext). Model(articleMockModel). Upsert(payload, opt) From 8366ad1c9a8ca61be8f76741832cc1b8ec7b56c5 Mon Sep 17 00:00:00 2001 From: Taufan Adhitya Date: Fri, 27 Sep 2024 19:25:01 +0700 Subject: [PATCH 09/13] fix: collection tests --- pkg/db/db_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/db/db_test.go b/pkg/db/db_test.go index 4e754073..0915cb3d 100644 --- a/pkg/db/db_test.go +++ b/pkg/db/db_test.go @@ -119,7 +119,7 @@ func TestSingle(t *testing.T) { } func TestGet(t *testing.T) { - var collection []ArticleMockModel + var collection []interface{} err := NewQuery(&mockRaidenContext).Model(articleMockModel).Get(&collection) assert.NoError(t, err) From e95abd7c5aefc067126f2c3e882e166725fc8848 Mon Sep 17 00:00:00 2001 From: Taufan Adhitya Date: Fri, 27 Sep 2024 19:26:54 +0700 Subject: [PATCH 10/13] fix: collection tests --- pkg/db/db_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/db/db_test.go b/pkg/db/db_test.go index 0915cb3d..c6c001a1 100644 --- a/pkg/db/db_test.go +++ b/pkg/db/db_test.go @@ -119,7 +119,7 @@ func TestSingle(t *testing.T) { } func TestGet(t *testing.T) { - var collection []interface{} + var collection interface{} err := NewQuery(&mockRaidenContext).Model(articleMockModel).Get(&collection) assert.NoError(t, err) From b7312f2fb58fae6ce5946e3f6a741ab898c92c4d Mon Sep 17 00:00:00 2001 From: Taufan Adhitya Date: Fri, 27 Sep 2024 19:28:21 +0700 Subject: [PATCH 11/13] fix: add AsSystem test --- pkg/db/db_test.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pkg/db/db_test.go b/pkg/db/db_test.go index c6c001a1..6eb71155 100644 --- a/pkg/db/db_test.go +++ b/pkg/db/db_test.go @@ -124,3 +124,10 @@ func TestGet(t *testing.T) { assert.NoError(t, err) } + +func TestByPass(t *testing.T) { + var collection interface{} + err := NewQuery(&mockRaidenContext).Model(articleMockModel).AsSystem().Get(&collection) + + assert.NoError(t, err) +} \ No newline at end of file From a88e78079bcb3f03efa48b05f778e3e98289b1aa Mon Sep 17 00:00:00 2001 From: Taufan Adhitya Date: Fri, 27 Sep 2024 19:30:01 +0700 Subject: [PATCH 12/13] fix: linter --- pkg/db/db_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/db/db_test.go b/pkg/db/db_test.go index 6eb71155..ff130b07 100644 --- a/pkg/db/db_test.go +++ b/pkg/db/db_test.go @@ -130,4 +130,4 @@ func TestByPass(t *testing.T) { err := NewQuery(&mockRaidenContext).Model(articleMockModel).AsSystem().Get(&collection) assert.NoError(t, err) -} \ No newline at end of file +} From 899f04ec804e1cb51ae45499098671ed5a9e269c Mon Sep 17 00:00:00 2001 From: Taufan Adhitya Date: Fri, 27 Sep 2024 19:32:32 +0700 Subject: [PATCH 13/13] fix: skip bypass on test --- pkg/db/postgrest_request.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pkg/db/postgrest_request.go b/pkg/db/postgrest_request.go index 27b68c09..a22c3a98 100644 --- a/pkg/db/postgrest_request.go +++ b/pkg/db/postgrest_request.go @@ -40,8 +40,10 @@ func PostgrestRequestBind(ctx raiden.Context, method string, url string, payload req.Header.SetMethod(method) if bypass { - req.Header.Set("apikey", getConfig().ServiceKey) - req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", getConfig().ServiceKey)) + if flag.Lookup("test.v") == nil { + req.Header.Set("apikey", getConfig().ServiceKey) + req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", getConfig().ServiceKey)) + } } else { apikey := string(ctx.RequestContext().Request.Header.Peek("apikey")) if apikey != "" {