Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a test for the trusted-bot feature #89

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading