-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: start bdd acceptance tests (#38)
- Loading branch information
1 parent
88179f6
commit 261442a
Showing
20 changed files
with
669 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
FROM golang:1.22.1-alpine3.19 | ||
|
||
WORKDIR /app | ||
|
||
COPY . . | ||
|
||
CMD ["go", "test", "-v"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package main | ||
|
||
func main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cucumber/godog" | ||
"github.com/tonitienda/kadai/acceptance-go/pkg/api" | ||
) | ||
|
||
type TestsImpl interface { | ||
UserNotLoggedIn(user string) error | ||
UserIsLoggedIn(user string) error | ||
UserRequestsListOfTasks(user string) error | ||
TheListOfTasksShouldBeEmpty() error | ||
UnauthorizedErrorReturned() error | ||
SuccessfulRequestReturned() error | ||
} | ||
|
||
func InitializeScenario(ctx *godog.ScenarioContext) { | ||
InitializeScenario2(ctx, api.NewApiTests()) | ||
} | ||
|
||
func InitializeScenario2(ctx *godog.ScenarioContext, impl TestsImpl) { | ||
|
||
ctx.Given(`^(.*) is not logged in$`, impl.UserNotLoggedIn) | ||
ctx.Given(`^(.*) is logged in$`, impl.UserIsLoggedIn) | ||
ctx.When(`^(.*) requests the list of tasks$`, impl.UserRequestsListOfTasks) | ||
ctx.Then(`^the list of tasks should be empty$`, impl.TheListOfTasksShouldBeEmpty) | ||
ctx.Then(`^the request should be refused because the user is unauthorized$`, impl.UnauthorizedErrorReturned) | ||
ctx.Then(`^the request should be successful$`, impl.SuccessfulRequestReturned) | ||
} | ||
|
||
func TestFeatures(t *testing.T) { | ||
suite := godog.TestSuite{ | ||
ScenarioInitializer: InitializeScenario, | ||
Options: &godog.Options{ | ||
Format: "pretty", | ||
Paths: []string{"features"}, | ||
TestingT: t, // Testing instance that will run subtests. | ||
}, | ||
} | ||
|
||
if suite.Run() != 0 { | ||
t.Fatal("non-zero status returned, failed to run feature tests") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
Feature: Access the list of tasks | ||
|
||
Scenario: Access without login should return a permission error | ||
Given Alice is not logged in | ||
When Alice requests the list of tasks | ||
Then the list of tasks should be empty | ||
And the request should be refused because the user is unauthorized | ||
|
||
|
||
|
||
Scenario: Access after login in should return a successful response | ||
Given Alice is logged in | ||
When Alice requests the list of tasks | ||
Then the list of tasks should be empty | ||
And the request should be successful |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module github.com/tonitienda/kadai/acceptance-go | ||
|
||
go 1.22.1 | ||
|
||
require ( | ||
github.com/cucumber/gherkin/go/v26 v26.2.0 // indirect | ||
github.com/cucumber/godog v0.14.0 // indirect | ||
github.com/cucumber/messages/go/v21 v21.0.1 // indirect | ||
github.com/gofrs/uuid v4.3.1+incompatible // indirect | ||
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect | ||
github.com/hashicorp/go-memdb v1.3.4 // indirect | ||
github.com/hashicorp/golang-lru v0.5.4 // indirect | ||
github.com/spf13/pflag v1.0.5 // indirect | ||
golang.org/x/oauth2 v0.19.0 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= | ||
github.com/cucumber/gherkin/go/v26 v26.2.0 h1:EgIjePLWiPeslwIWmNQ3XHcypPsWAHoMCz/YEBKP4GI= | ||
github.com/cucumber/gherkin/go/v26 v26.2.0/go.mod h1:t2GAPnB8maCT4lkHL99BDCVNzCh1d7dBhCLt150Nr/0= | ||
github.com/cucumber/godog v0.14.0 h1:h/K4t7XBxsFBF+UJEahNqJ1/2VHVepRXCSq3WWWnehs= | ||
github.com/cucumber/godog v0.14.0/go.mod h1:FX3rzIDybWABU4kuIXLZ/qtqEe1Ac5RdXmqvACJOces= | ||
github.com/cucumber/messages/go/v21 v21.0.1 h1:wzA0LxwjlWQYZd32VTlAVDTkW6inOFmSM+RuOwHZiMI= | ||
github.com/cucumber/messages/go/v21 v21.0.1/go.mod h1:zheH/2HS9JLVFukdrsPWoPdmUtmYQAQPLk7w5vWsk5s= | ||
github.com/cucumber/messages/go/v22 v22.0.0/go.mod h1:aZipXTKc0JnjCsXrJnuZpWhtay93k7Rn3Dee7iyPJjs= | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/gofrs/uuid v4.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= | ||
github.com/gofrs/uuid v4.3.1+incompatible h1:0/KbAdpx3UXAx1kEOWHJeOkpbgRFGHVgv+CFIY7dBJI= | ||
github.com/gofrs/uuid v4.3.1+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= | ||
github.com/hashicorp/go-immutable-radix v1.3.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60= | ||
github.com/hashicorp/go-immutable-radix v1.3.1 h1:DKHmCUm2hRBK510BaiZlwvpD40f8bJFeZnpfm2KLowc= | ||
github.com/hashicorp/go-immutable-radix v1.3.1/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60= | ||
github.com/hashicorp/go-memdb v1.3.4 h1:XSL3NR682X/cVk2IeV0d70N4DZ9ljI885xAEU8IoK3c= | ||
github.com/hashicorp/go-memdb v1.3.4/go.mod h1:uBTr1oQbtuMgd1SSGoR8YV27eT3sBHbYiNm53bMpgSg= | ||
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= | ||
github.com/hashicorp/go-uuid v1.0.2/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= | ||
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= | ||
github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc= | ||
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= | ||
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= | ||
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= | ||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= | ||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= | ||
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0= | ||
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= | ||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= | ||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= | ||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= | ||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= | ||
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= | ||
golang.org/x/oauth2 v0.19.0 h1:9+E/EZBCbTLNrbN35fHv/a/d/mOBatymz1zbtQrXpIg= | ||
golang.org/x/oauth2 v0.19.0/go.mod h1:vYi7skDa1x015PmRRYZ7+s1cWyPgrPiSYRe4rnsexc8= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package api | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"os" | ||
) | ||
|
||
type Task struct { | ||
ID string `json:"id"` | ||
Title string `json:"title"` | ||
} | ||
|
||
type ApiTests struct { | ||
UserTokens map[string]string | ||
UserTasks map[string][]Task | ||
LastRequestedTasks []Task | ||
LastRequestStatus int | ||
BackendUrl string | ||
} | ||
|
||
func NewApiTests() *ApiTests { | ||
return &ApiTests{ | ||
UserTokens: map[string]string{}, | ||
LastRequestedTasks: []Task{}, | ||
BackendUrl: os.Getenv("BACKEND_BASE_URL"), | ||
} | ||
} | ||
|
||
func (a *ApiTests) UserNotLoggedIn(user string) error { | ||
delete(a.UserTokens, user) | ||
return nil | ||
} | ||
|
||
func (a *ApiTests) UserIsLoggedIn(user string) error { | ||
fmt.Println(user, " logged in") | ||
token, err := userLogin(user) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
a.UserTokens[user] = token | ||
return nil | ||
} | ||
|
||
func (a *ApiTests) UserRequestsListOfTasks(user string) error { | ||
url := fmt.Sprintf("%s/v0/tasks", a.BackendUrl) | ||
|
||
client := &http.Client{} | ||
req, _ := http.NewRequest("GET", url, nil) | ||
|
||
token, isUserLoggedIn := a.UserTokens[user] | ||
|
||
fmt.Printf("User tokens: %v", a.UserTokens) | ||
if isUserLoggedIn { | ||
fmt.Println("User", user, "is logged in") | ||
req.Header.Add("Authorization", "Bearer "+token) | ||
} | ||
|
||
res, err := client.Do(req) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
a.LastRequestStatus = res.StatusCode | ||
|
||
if res.StatusCode == http.StatusOK { | ||
// Deserialize tasks | ||
fmt.Println("correct") | ||
} | ||
return nil | ||
} | ||
func (a *ApiTests) TheListOfTasksShouldBeEmpty() error { | ||
if len(a.LastRequestedTasks) != 0 { | ||
return fmt.Errorf("the list of tasks should be empty") | ||
} | ||
return nil | ||
} | ||
func (a *ApiTests) UnauthorizedErrorReturned() error { | ||
if a.LastRequestStatus != http.StatusUnauthorized { | ||
return fmt.Errorf("expected status %d but %d was returned", http.StatusUnauthorized, a.LastRequestStatus) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (a *ApiTests) SuccessfulRequestReturned() error { | ||
if a.LastRequestStatus != http.StatusOK { | ||
return fmt.Errorf("expected status %d but %d was returned", http.StatusOK, a.LastRequestStatus) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"os" | ||
"strings" | ||
) | ||
|
||
func userLogin(user string) (string, error) { | ||
|
||
url := os.Getenv("AUTH0_ISSUER_BASE_URL") + "/oauth/token" | ||
|
||
payload := strings.NewReader("{\"client_id\":\"" + os.Getenv("AUTH0_KADAI_BACKEND_CLIENT_ID") + "\",\"client_secret\":\"" + os.Getenv("AUTH0_KADAI_BACKEND_CLIENT_SECRET") + "\",\"audience\":\"" + os.Getenv("AUTH0_AUDIENCE") + "\",\"grant_type\":\"client_credentials\"}") | ||
|
||
req, _ := http.NewRequest("POST", url, payload) | ||
|
||
req.Header.Add("content-type", "application/json") | ||
|
||
res, _ := http.DefaultClient.Do(req) | ||
|
||
defer res.Body.Close() | ||
body, _ := io.ReadAll(res.Body) | ||
|
||
fmt.Println(res) | ||
fmt.Println(string(body)) | ||
|
||
var data map[string]interface{} | ||
|
||
err := json.Unmarshal(body, &data) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
fmt.Printf("OK: data=%#v\n", data) | ||
|
||
token, ok := data["access_token"] | ||
|
||
if !ok { | ||
return "", fmt.Errorf("the token could not be obtained") | ||
} | ||
|
||
return fmt.Sprint(token), nil | ||
} |
Oops, something went wrong.