wip
This commit is contained in:
18
mangezmieux-backend/internal/godog/model.go
Normal file
18
mangezmieux-backend/internal/godog/model.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package godog
|
||||
|
||||
import "context"
|
||||
|
||||
type TestCenter interface {
|
||||
GetFunctionalityContext() any
|
||||
}
|
||||
type genericTestCenter struct {
|
||||
testCenter TestCenter
|
||||
}
|
||||
|
||||
type ResourceHandler interface {
|
||||
Create(ctx context.Context, val string) (context.Context, error)
|
||||
Read(ctx context.Context, val string) (context.Context, error)
|
||||
Update(ctx context.Context, val string) (context.Context, error)
|
||||
Patch(ctx context.Context, val string) (context.Context, error)
|
||||
Delete(ctx context.Context, val string) (context.Context, error)
|
||||
}
|
||||
23
mangezmieux-backend/internal/godog/resources.go
Normal file
23
mangezmieux-backend/internal/godog/resources.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package godog
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
)
|
||||
|
||||
var resourcesHandler = map[string]ResourceHandler{}
|
||||
|
||||
func RegisterResourceHandler(resourceName string, handler ResourceHandler) {
|
||||
if _, ok := resourcesHandler[resourceName]; ok {
|
||||
log.Fatalf("Can't add an already existing handler for resource %s", resourceName)
|
||||
return
|
||||
}
|
||||
resourcesHandler[resourceName] = handler
|
||||
}
|
||||
|
||||
func GetResourceHandler(resourceName string) ResourceHandler {
|
||||
if val, ok := resourcesHandler[resourceName]; ok {
|
||||
return val
|
||||
}
|
||||
panic(fmt.Sprintf("no handler found for resource type %s", resourceName))
|
||||
}
|
||||
92
mangezmieux-backend/internal/godog/sentence.go
Normal file
92
mangezmieux-backend/internal/godog/sentence.go
Normal 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
|
||||
}
|
||||
20
mangezmieux-backend/internal/godog/setup.go
Normal file
20
mangezmieux-backend/internal/godog/setup.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package godog
|
||||
|
||||
import cucumber "github.com/cucumber/godog"
|
||||
|
||||
func Setup(ctx *cucumber.ScenarioContext, testCenter TestCenter) {
|
||||
gtc := genericTestCenter{testCenter: testCenter}
|
||||
ctx.Step(`^the resource is created`, gtc.theResourceIsCreated)
|
||||
ctx.Step(`^the response indicates that the ([^\s]+) already exists`, gtc.theResourceAlreadyExists)
|
||||
ctx.Step(`^the response indicates that the user doesn't have right`, gtc.theUserMissRight)
|
||||
ctx.Step(`^the response indicates that this a bad request`, gtc.badRequest)
|
||||
ctx.Step(`^the response indicates that this a forbidden request`, gtc.forbiddenRequest)
|
||||
ctx.Step(`^the response has status (\d+)$`, gtc.theResponseHasStatus)
|
||||
ctx.Step(`^the ([^\s]+) ([^\s]+) doesn\'t exist yet`, gtc.resourceDoesntExist)
|
||||
ctx.Step(`^the field ([^\s]+) is (\d+)$`, gtc.theFieldHasValue)
|
||||
ctx.Step(`^the field ([^\s]+) has value "([^"]*)"$`, gtc.theFieldHasValue)
|
||||
ctx.Step(`^the user create a[n]* ([^\s]+) with the following data:$`, gtc.theUserCreatesAResourcesWithTheFollowingData)
|
||||
ctx.Step(`^the user update a[n]* ([^\s]+) with the following data:$`, gtc.theUserUpdatesAResourcesWithTheFollowingData)
|
||||
ctx.Step(`^the user patch a[n]* ([^\s]+) with the following data:$`, gtc.theUserUpdatesAResourcesWithTheFollowingData)
|
||||
ctx.Step(`^the ([^\s]+) exists with the following data:$`, gtc.theResourceExistWithTheFollowingData)
|
||||
}
|
||||
Reference in New Issue
Block a user