Skip to content

Commit

Permalink
test: added tests related to error handling for the yahoo client
Browse files Browse the repository at this point in the history
  • Loading branch information
achannarasappa committed Jun 7, 2023
1 parent 16bce70 commit 7ba4cf6
Showing 1 changed file with 142 additions and 3 deletions.
145 changes: 142 additions & 3 deletions internal/quote/yahoo/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,21 +102,157 @@ var _ = Describe("Client", func() {

Describe("getCookieEU", func() {

When("the client is redirected to consent to EU cookie policy", func() {
It("should accept the policy and set the cookie", func() {

It("should accept the policy and set the cookie", func() {
var clientRestyAlt = resty.New()
httpmock.ActivateNonDefault(clientRestyAlt.GetClient())
defer httpmock.DeactivateAndReset()

mockResponseForEUConsentRedirect()
mockResponseForEUConsentRedirectSessionId()
mockResponseForEUConsentAccept()
mockResponseForCrumbSuccess()

outputErr := c.RefreshSession(resty.New(), clientRestyAlt)

Expect(outputErr).ToNot(HaveOccurred())

})

When("there is an unexpected response when getting the session id and CSRF token", func() {

It("should return an error", func() {

var clientRestyAlt = resty.New()
httpmock.ActivateNonDefault(clientRestyAlt.GetClient())
defer httpmock.DeactivateAndReset()

mockResponseForEUConsentRedirect()
mockResponseHTTPError("GET", "https://guce.yahoo.com/consent")
mockResponseForEUConsentAccept()
mockResponseForCrumbSuccess()

outputErr := c.RefreshSession(resty.New(), clientRestyAlt)

Expect(outputErr).To(HaveOccurred())

})

})

When("there is a client error", func() {

It("should return an error", func() {

var clientRestyAlt = resty.New()
httpmock.ActivateNonDefault(clientRestyAlt.GetClient())
defer httpmock.DeactivateAndReset()

mockResponseForEUConsentRedirect()
mockResponseClientError("GET", "https://guce.yahoo.com/consent")
mockResponseForEUConsentAccept()
mockResponseForCrumbSuccess()

outputErr := c.RefreshSession(resty.New(), clientRestyAlt)

Expect(outputErr).To(HaveOccurred())

})

})

When("there is no session id in the redirect URL", func() {

It("should return an error", func() {

var clientRestyAlt = resty.New()
httpmock.ActivateNonDefault(clientRestyAlt.GetClient())
defer httpmock.DeactivateAndReset()

mockResponseForEUConsentRedirect()

httpmock.RegisterResponder("GET", "https://guce.yahoo.com/consent", func(request *http.Request) (*http.Response, error) {

response := httpmock.NewStringResponse(http.StatusFound, "")
response.Header.Set("Location", "https://consent.yahoo.com/collectConsent?lang=en-US&inline=false")
response.Request = request

return response, nil
})

httpmock.RegisterResponder("GET", "https://consent.yahoo.com/collectConsent", func(request *http.Request) (*http.Response, error) {

response := httpmock.NewStringResponse(http.StatusOK, "")

response.Request = request
return response, nil
})

mockResponseForEUConsentAccept()
mockResponseForCrumbSuccess()

outputErr := c.RefreshSession(resty.New(), clientRestyAlt)

Expect(outputErr).ToNot(HaveOccurred())
Expect(outputErr).To(HaveOccurred())

})

})

When("there is no CSRF token in the Location header", func() {

It("should return an error", func() {

var clientRestyAlt = resty.New()
httpmock.ActivateNonDefault(clientRestyAlt.GetClient())
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder("GET", "https://finance.yahoo.com/", func(request *http.Request) (*http.Response, error) {

response := httpmock.NewStringResponse(http.StatusTemporaryRedirect, "")
response.Header.Set("Location", "https://guce.yahoo.com/consent?brandType=nonEu")
response.Header.Set("Set-Cookie", "GUCS=TUygQ0Y0; Max-Age=1800; Domain=.yahoo.com; Path=/; Secure")

response.Request = request
return response, nil
})

mockResponseForEUConsentRedirectSessionId()
mockResponseForEUConsentAccept()
mockResponseForCrumbSuccess()

outputErr := c.RefreshSession(resty.New(), clientRestyAlt)

Expect(outputErr).To(HaveOccurred())

})

})

When("there is no GUCS cookie set", func() {

It("should return an error", func() {

var clientRestyAlt = resty.New()
httpmock.ActivateNonDefault(clientRestyAlt.GetClient())
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder("GET", "https://finance.yahoo.com/", func(request *http.Request) (*http.Response, error) {

response := httpmock.NewStringResponse(http.StatusTemporaryRedirect, "")
response.Header.Set("Location", "https://guce.yahoo.com/consent?brandType=nonEu&gcrumb=HJBaI422")

response.Request = request
return response, nil
})

mockResponseForEUConsentRedirectSessionId()
mockResponseForEUConsentAccept()
mockResponseForCrumbSuccess()

outputErr := c.RefreshSession(resty.New(), clientRestyAlt)

Expect(outputErr).To(HaveOccurred())

})

Expand Down Expand Up @@ -215,7 +351,9 @@ func mockResponseForEUConsentRedirect() {
response.Request = request
return response, nil
})
}

func mockResponseForEUConsentRedirectSessionId() {
httpmock.RegisterResponder("GET", "https://guce.yahoo.com/consent", func(request *http.Request) (*http.Response, error) {

response := httpmock.NewStringResponse(http.StatusFound, "")
Expand Down Expand Up @@ -248,6 +386,7 @@ func mockResponseForEUConsentAccept() {
httpmock.RegisterResponder("GET", "https://guce.yahoo.com/copyConsent", func(request *http.Request) (*http.Response, error) {

response := httpmock.NewStringResponse(http.StatusFound, "")
response.Header.Set("Location", "https://finance.yahoo.com/")
response.Header.Set("Set-Cookie", "A3=d=AQABBPMJfWQCWPnJSAFIwq1PtsjJQ_yNsJ8FEgEBAQFbfmSGZNxN0iMA_eMAAA&S=AQAAAk_fgKYu72Cro5IHlbBd6yg; Expires=Tue, 4 Jun 2024 04:02:28 GMT; Max-Age=31557600; Domain=.yahoo.com; Path=/; SameSite=None; Secure; HttpOnly")

response.Request = request
Expand Down

0 comments on commit 7ba4cf6

Please sign in to comment.