This commit is contained in:
2024-07-19 17:04:42 +02:00
commit 5e0d0ec69f
71 changed files with 3316 additions and 0 deletions

View File

@@ -0,0 +1,92 @@
package godog
import (
"context"
"encoding/json"
"fmt"
"github.com/cucumber/godog"
"github.com/ohler55/ojg/jp"
"net/http/httptest"
)
func (gtc *genericTestCenter) theResourceIsCreated(ctx context.Context) (context.Context, error) {
return gtc.theResponseHasStatus(ctx, 201)
}
func (gtc *genericTestCenter) theResourceAlreadyExists(ctx context.Context) (context.Context, error) {
return gtc.theResponseHasStatus(ctx, 409)
}
func (gtc *genericTestCenter) theUserMissRight(ctx context.Context) (context.Context, error) {
return gtc.theResponseHasStatus(ctx, 403)
}
func (gtc *genericTestCenter) badRequest(ctx context.Context) (context.Context, error) {
return gtc.theResponseHasStatus(ctx, 400)
}
func (gtc *genericTestCenter) forbiddenRequest(ctx context.Context) (context.Context, error) {
return gtc.theResponseHasStatus(ctx, 403)
}
func (gtc *genericTestCenter) theResponseHasStatus(ctx context.Context, status int) (context.Context, error) {
httpRecorder := ctx.Value("recorder").(*httptest.ResponseRecorder)
if httpRecorder.Code != status {
return ctx, fmt.Errorf("got code %d with body %s", httpRecorder.Code, httpRecorder.Body.String())
}
ctx = context.WithValue(ctx, "OBJECT_RESPONSE", httpRecorder.Body.String())
return ctx, nil
}
func (gtc *genericTestCenter) theFieldHasValue(ctx context.Context, fieldName, value string) (context.Context, error) {
compiledPath, err := jp.ParseString(fieldName)
body := ctx.Value("OBJECT_RESPONSE").(string)
if err != nil {
return ctx, err
}
var resourceAsMap interface{}
err = json.Unmarshal([]byte(body), &resourceAsMap)
if err != nil {
return ctx, err
}
datas := compiledPath.Get(resourceAsMap)
if len(datas) != 1 {
return ctx, fmt.Errorf("Found %v data. Expected only one", len(datas))
}
if datas[0] != value {
return ctx, fmt.Errorf("The field %s has value %s . Expected %s", fieldName, datas[0], value)
}
return ctx, nil
}
func (gtc *genericTestCenter) resourceDoesntExist(ctx context.Context, resourceType string, resourceName string) (context.Context, error) {
handler := GetResourceHandler(resourceType)
ctx, _ = handler.Delete(ctx, resourceName)
return ctx, nil
}
func (gtc *genericTestCenter) theUserCreatesAResourcesWithTheFollowingData(ctx context.Context, resourceType string, content *godog.DocString) (context.Context, error) {
handler := GetResourceHandler(resourceType)
ctx, _ = handler.Create(ctx, content.Content)
return ctx, nil
}
func (gtc *genericTestCenter) theUserPatchesAResourcesWithTheFollowingData(ctx context.Context, resourceType string, content *godog.DocString) (context.Context, error) {
handler := GetResourceHandler(resourceType)
ctx, _ = handler.Patch(ctx, content.Content)
return ctx, nil
}
func (gtc *genericTestCenter) theUserUpdatesAResourcesWithTheFollowingData(ctx context.Context, resourceType string, content *godog.DocString) (context.Context, error) {
handler := GetResourceHandler(resourceType)
ctx, _ = handler.Update(ctx, content.Content)
return ctx, nil
}
func (gtc *genericTestCenter) theResourceExistWithTheFollowingData(ctx context.Context, resourceType string, content *godog.DocString) (context.Context, error) {
handler := GetResourceHandler(resourceType)
ctx, _ = handler.Create(ctx, content.Content)
return ctx, nil
}