Skip to content

Commit

Permalink
Merge pull request #89 from silinternational/test-trusted-bot
Browse files Browse the repository at this point in the history
add a test for the trusted-bot feature
  • Loading branch information
briskt authored Dec 13, 2024
2 parents 8d82421 + 3c39cff commit a6e3459
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
5 changes: 5 additions & 0 deletions features/auth.feature
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,8 @@ Feature: functional test cases
| url | one |
| url | two |
| url | three |

Scenario: Trusted bot
When we send a request with a user-agent that is trusted
Then we do not see an error message
And we will see the default version of the website
19 changes: 14 additions & 5 deletions functional_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,15 @@ func InitializeScenario(ctx *godog.ScenarioContext) {
ctx.Step(`^we send a request with (\w+) authorization data$`, weSendARequestWithAuthorizationData)
ctx.Step(`^we send a request with authorization data in the (\w+) authorizing (\w+) access$`,
weSendARequestWithAuthorizationDataAuthorizingAccess)
ctx.Step(`^we send a request with a user-agent that is trusted$`, weSendARequestWithAUserAgentThatIsTrusted)
ctx.Step(`^we will be redirected to the management api$`, weWillBeRedirectedToTheManagementApi)
ctx.Step(`^we do not see an error message$`, weDoNotSeeAnErrorMessage)
ctx.Step(`^we will see an error message$`, weWillSeeAnErrorMessage)
ctx.Step(`^we will see the (\w+) version of the website$`, weWillSeeTheAccessLevelVersionOfTheWebsite)
ctx.Step(`^we do not see the token parameter$`, weDoNotSeeTheTokenParameter)
}

func sendRequest(url string, c *http.Cookie) error {
func sendRequest(url string, c *http.Cookie, headers map[string]string) error {
request, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return err
Expand All @@ -69,6 +70,10 @@ func sendRequest(url string, c *http.Cookie) error {
request.AddCookie(c)
}

for k, v := range headers {
request.Header.Set(k, v)
}

response, err := client.Do(request)
if err != nil {
return err
Expand All @@ -95,7 +100,11 @@ func weSendARequestWithAuthorizationDataAuthorizingAccess(where, level string) e
} else {
url += fmt.Sprintf("?%s=%s", p.TokenParam, token)
}
return sendRequest(url, c)
return sendRequest(url, c, nil)
}

func weSendARequestWithAUserAgentThatIsTrusted() error {
return sendRequest(p.Host, nil, map[string]string{"User-Agent": "googlebot"})
}

func weSendARequestWithAuthorizationData(t string) error {
Expand All @@ -112,7 +121,7 @@ func weSendARequestWithAuthorizationData(t string) error {
default:
return godog.ErrPending
}
return sendRequest(p.Host, c)
return sendRequest(p.Host, c, nil)
}

func weWillBeRedirectedToTheManagementApi() error {
Expand All @@ -131,8 +140,8 @@ func weWillSeeAnErrorMessage() error {

func weWillSeeTheAccessLevelVersionOfTheWebsite(level string) error {
proxy := last
if err := sendRequest("http://"+p.Sites[level], nil); err != nil {
return err
if err := sendRequest("http://"+p.getSite(level), nil, nil); err != nil {
return fmt.Errorf("sendRequest failed: %w", err)
}

return assertEqual(last.body, proxy.body)
Expand Down

0 comments on commit a6e3459

Please sign in to comment.