diff --git a/internal/quote/yahoo/client/client_test.go b/internal/quote/yahoo/client/client_test.go index 11f3b67..9cae5f1 100644 --- a/internal/quote/yahoo/client/client_test.go +++ b/internal/quote/yahoo/client/client_test.go @@ -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()) }) @@ -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, "") @@ -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