Files
upstream-corteza/server/tests/helpers/assert.go
upstream-mirror 7e385fc126
Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s
snapshot of https://github.com/cortezaproject/corteza.git (2024.9.x, 2026-09-18; blobs > 131072B omitted)
2026-09-18 01:22:23 +05:00

173 lines
3.7 KiB
Go

package helpers
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"
"github.com/cortezaproject/corteza/server/compose/types"
)
type (
assertFn func(*http.Response, *http.Request) error
StdErrorResponse struct {
Error struct {
Message string
}
}
RecordValueErrorSetResponse struct {
Error struct {
Message string
Details []types.RecordValueError
}
}
)
// decodes response body to given struct
func DecodeBody(rsp *http.Response, s interface{}) error {
if err := json.NewDecoder(rsp.Body).Decode(&s); err != nil {
return fmt.Errorf("could not decode body: %w", err)
}
return nil
}
// Returns first input that could be an error
func firstErr(ee ...interface{}) error {
for _, e := range ee {
switch t := e.(type) {
case error:
if t != nil {
return t
}
case StdErrorResponse:
if t.Error.Message != "" {
return errors.New(t.Error.Message)
}
case string:
if t != "" {
return errors.New(t)
}
}
}
return nil
}
// AssertNoErrors ensures there are no errors in the response
func AssertNoErrors(rsp *http.Response, _ *http.Request) (err error) {
tmp := StdErrorResponse{}
return firstErr(DecodeBody(rsp, &tmp), tmp)
}
// Asserts that all expected errors are returned
//
// # Compares each error by Kind, Message and Meta.field
//
// Note: This assertion always expects errors!
func AssertRecordValueError(exp ...*types.RecordValueError) assertFn {
return func(rsp *http.Response, _ *http.Request) (err error) {
rcvd := RecordValueErrorSetResponse{}
if err = DecodeBody(rsp, &rcvd); err != nil {
return err
}
if len(rcvd.Error.Details) == 0 {
return fmt.Errorf("expecting value errors, none received")
}
expLoop:
for _, e := range exp {
for _, r := range rcvd.Error.Details {
if e.Kind != r.Kind {
continue
}
if e.Message != r.Message {
continue
}
if e.Meta["field"] != r.Meta["field"] {
continue
}
// found expected error
continue expLoop
}
// did not find expected error
return fmt.Errorf("did not find expected error %v", e)
}
return nil
}
}
// Dump can be put into Assert()
func Dump(rsp *http.Response, _ *http.Request) (err error) {
var payload interface{}
if err = DecodeBody(rsp, &payload); err != nil {
return err
}
return nil
}
// AssertError ensures there are no errors in the response
func AssertError(expectedError string) assertFn {
return func(rsp *http.Response, _ *http.Request) (err error) {
tmp := StdErrorResponse{}
if err = DecodeBody(rsp, &tmp); err != nil {
return err
}
if tmp.Error.Message == "" {
return fmt.Errorf("No error, expecting: %v", expectedError)
}
if expectedError != tmp.Error.Message {
return fmt.Errorf("Expecting error %v, got: %v", expectedError, tmp.Error.Message)
}
return nil
}
}
// AssertBody compares the raw body to the provided string
func AssertBody(expected string) assertFn {
return func(rsp *http.Response, _ *http.Request) (err error) {
bb, err := ioutil.ReadAll(rsp.Body)
if err != nil {
return err
}
got := strings.Trim(string(bb), " \n")
if expected != got {
return fmt.Errorf("Expecting: %v, got: %v", expected, got)
}
return nil
}
}
// AssertErrorP checks if the expected error is part of the error message
func AssertErrorP(expectedError string) assertFn {
return func(rsp *http.Response, _ *http.Request) (err error) {
tmp := StdErrorResponse{}
if err = DecodeBody(rsp, &tmp); err != nil {
return err
}
if tmp.Error.Message == "" {
return fmt.Errorf("No error, expecting error with: %v", expectedError)
}
if !strings.Contains(tmp.Error.Message, expectedError) {
return fmt.Errorf("Expecting error with %v, got: %v", expectedError, tmp.Error.Message)
}
return nil
}
}