-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Gherkin feature tests for API Key support
- Loading branch information
Showing
36 changed files
with
296 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
Feature: Api Key | ||
|
||
Scenario: A configured api key is sent in the Authorization header | ||
Given an agent | ||
When an api key is set to 'RTNxMjlXNEJt' in the config | ||
Then the Authorization header is 'ApiKey RTNxMjlXNEJt' | ||
|
||
Scenario: A configured api key takes precedence over a secret token | ||
Given an agent | ||
When an api key is set in the config | ||
And a secret_token is set in the config | ||
Then the api key is sent in the Authorization header | ||
|
||
Scenario: A configured secret token is sent if no api key is configured | ||
Given an agent | ||
When a secret_token is set in the config | ||
And an api key is not set in the config | ||
Then the secret token is sent in the Authorization header |
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,118 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package apmgodog | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"testing" | ||
|
||
"github.com/DATA-DOG/godog" | ||
"github.com/DATA-DOG/godog/colors" | ||
"github.com/DATA-DOG/godog/gherkin" | ||
) | ||
|
||
// Run runs the Gherkin feature files specified in paths as Go subtests. | ||
func Run(t *testing.T, paths []string) { | ||
initContext := func(s *godog.Suite) { | ||
var commands chan command | ||
var scenarioFailed bool | ||
s.BeforeFeature(func(f *gherkin.Feature) { | ||
commands = make(chan command) | ||
go runCommands(t, commands) | ||
startTest(commands, f.Name) | ||
}) | ||
s.AfterFeature(func(f *gherkin.Feature) { | ||
endTest(commands, nil) | ||
close(commands) | ||
}) | ||
s.BeforeScenario(func(s interface{}) { | ||
scenarioFailed = false | ||
switch s := s.(type) { | ||
case *gherkin.Scenario: | ||
startTest(commands, s.Name) | ||
case *gherkin.ScenarioOutline: | ||
startTest(commands, s.Name) | ||
} | ||
}) | ||
s.AfterScenario(func(_ interface{}, err error) { | ||
endTest(commands, err) | ||
}) | ||
s.BeforeStep(func(step *gherkin.Step) { | ||
if scenarioFailed { | ||
fmt.Printf(colors.Yellow(" %s%s\n"), step.Keyword, step.Text) | ||
} | ||
}) | ||
s.AfterStep(func(step *gherkin.Step, err error) { | ||
if err != nil { | ||
scenarioFailed = true | ||
fmt.Printf(colors.Red(" %s%s (%s)\n"), step.Keyword, step.Text, err) | ||
} else { | ||
fmt.Printf(colors.Cyan(" %s%s\n"), step.Keyword, step.Text) | ||
} | ||
}) | ||
InitContext(s) | ||
} | ||
|
||
godog.RunWithOptions("godog", initContext, godog.Options{ | ||
Format: "events", // must pick one, this will do | ||
Paths: []string{"."}, | ||
Output: ioutil.Discard, | ||
}) | ||
} | ||
|
||
func startTest(commands chan command, name string) { | ||
commands <- func(t *testing.T) error { | ||
t.Run(name, func(t *testing.T) { | ||
runCommands(t, commands) | ||
}) | ||
return nil | ||
} | ||
} | ||
|
||
func endTest(commands chan command, err error) { | ||
commands <- func(t *testing.T) error { | ||
if err != nil { | ||
return err | ||
} | ||
return done{} | ||
} | ||
} | ||
|
||
func runCommands(t *testing.T, commands chan command) { | ||
for { | ||
cmd, ok := <-commands | ||
if !ok { | ||
return | ||
} | ||
err := cmd(t) | ||
switch err.(type) { | ||
case nil: | ||
case done: | ||
return | ||
default: | ||
t.Fatal(err) | ||
} | ||
} | ||
} | ||
|
||
type command func(t *testing.T) error | ||
|
||
type done struct{} | ||
|
||
func (done) Error() string { return "done" } |
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,122 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package apmgodog | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"strings" | ||
|
||
"github.com/DATA-DOG/godog" | ||
|
||
"go.elastic.co/apm" | ||
"go.elastic.co/apm/transport" | ||
) | ||
|
||
const ( | ||
aSecretToken = "a_secret_token" | ||
anAPIKey = "an_api_key" | ||
) | ||
|
||
type featureContext struct { | ||
apiKey string | ||
secretToken string | ||
} | ||
|
||
// InitContext initialises a godoc.Suite with step definitions. | ||
func InitContext(s *godog.Suite) { | ||
c := &featureContext{} | ||
s.BeforeScenario(func(interface{}) { c.reset() }) | ||
|
||
s.Step("^an agent$", c.anAgent) | ||
s.Step("^an api key is not set in the config$", func() error { return nil }) | ||
s.Step("^an api key is set in the config$", func() error { return c.setAPIKey(anAPIKey) }) | ||
s.Step("^an api key is set to '(.*)' in the config$", c.setAPIKey) | ||
s.Step("^a secret_token is set in the config$", func() error { return c.setSecretToken(aSecretToken) }) | ||
s.Step("^the Authorization header is '(.*)'$", c.checkAuthorizationHeader) | ||
s.Step("^the secret token is sent in the Authorization header$", c.secretTokenSentInAuthorizationHeader) | ||
s.Step("^the api key is sent in the Authorization header$", c.apiKeySentInAuthorizationHeader) | ||
} | ||
|
||
func (c *featureContext) reset() { | ||
c.apiKey = "" | ||
c.secretToken = "" | ||
for _, k := range os.Environ() { | ||
if strings.HasPrefix(k, "ELASTIC_APM") { | ||
os.Unsetenv(k) | ||
} | ||
} | ||
} | ||
|
||
func (c *featureContext) anAgent() error { | ||
// No-op; we create the tracer as needed to test steps. | ||
return nil | ||
} | ||
|
||
func (c *featureContext) setAPIKey(v string) error { | ||
c.apiKey = v | ||
return nil | ||
} | ||
|
||
func (c *featureContext) setSecretToken(v string) error { | ||
c.secretToken = v | ||
return nil | ||
} | ||
|
||
func (c *featureContext) secretTokenSentInAuthorizationHeader() error { | ||
return c.checkAuthorizationHeader("Bearer " + c.secretToken) | ||
} | ||
|
||
func (c *featureContext) apiKeySentInAuthorizationHeader() error { | ||
return c.checkAuthorizationHeader("ApiKey " + c.apiKey) | ||
} | ||
|
||
func (c *featureContext) checkAuthorizationHeader(expected string) error { | ||
var authHeader []string | ||
var h http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) { | ||
authHeader = r.Header["Authorization"] | ||
} | ||
server := httptest.NewServer(h) | ||
defer server.Close() | ||
|
||
os.Setenv("ELASTIC_APM_SECRET_TOKEN", c.secretToken) | ||
os.Setenv("ELASTIC_APM_API_KEY", c.apiKey) | ||
os.Setenv("ELASTIC_APM_SERVER_URL", server.URL) | ||
if _, err := transport.InitDefault(); err != nil { | ||
return err | ||
} | ||
|
||
tracer, err := apm.NewTracer("godog", "") | ||
if err != nil { | ||
return err | ||
} | ||
defer tracer.Close() | ||
|
||
tracer.StartTransaction("name", "type").End() | ||
tracer.Flush(nil) | ||
|
||
if n := len(authHeader); n != 1 { | ||
return fmt.Errorf("got %d Authorization headers, expected 1", n) | ||
} | ||
if authHeader[0] != expected { | ||
return fmt.Errorf("got Authorization header value %q, expected %q", authHeader, expected) | ||
} | ||
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
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
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
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
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
Oops, something went wrong.