func newClient(cmd *cobra.Command) wl.Client { var l logger.Logger if verbose { l = logger.NewLogger(logger.DEBUG) } else { l = logger.NewLogger(logger.INFO) } if accessToken == "" { accessToken = os.Getenv(accessTokenEnvVariable) } if accessToken == "" { l.Error( "exiting", errors.New("accessToken not found. Either provide the flag -"+accessTokenLongFlag+" or set the environment variable "+accessTokenEnvVariable)) os.Exit(2) } if clientID == "" { clientID = os.Getenv(clientIDEnvVariable) } if clientID == "" { l.Error( "exiting", errors.New("clientID not found. Either provide the flag -"+clientIDLongFlag+" or set the environment variable "+clientIDEnvVariable)) os.Exit(2) } return oauth.NewClient(accessToken, clientID, wl.APIURL, l) }
func (h handler) Tasks(w http.ResponseWriter, r *http.Request) { session, err := h.store.Get(r, "session-name") if err != nil { h.logger.Error("", err) http.Error(w, err.Error(), 500) return } accessTokenInterface := session.Values["accessToken"] if accessTokenInterface == nil { err := fmt.Errorf("accessToken not found in session") h.logger.Error("", err) http.Error(w, err.Error(), 500) return } accessToken, ok := accessTokenInterface.(string) if !ok { err := fmt.Errorf("failed to convert %v into string", accessTokenInterface) h.logger.Error("", err) http.Error(w, err.Error(), 500) return } if accessToken == "" { err := fmt.Errorf("accessToken empty in session") h.logger.Error("", err) http.Error(w, err.Error(), 500) return } client := oauth.NewClient( accessToken, h.clientID, wl.APIURL, logger.NewLogger(logger.INFO), ) completed := true completedTasks, err := client.CompletedTasks(completed) if err != nil { fmt.Printf("err getting tasks: %s\n", err.Error()) } tasks, err := tardyTasks(completedTasks) if err != nil { fmt.Printf("err converting tasks: %s\n", err.Error()) } err = json.NewEncoder(w).Encode(tasks) if err != nil { fmt.Printf("err serializing completed: %s\n", err.Error()) } }
server.AppendHandlers( ghttp.CombineHandlers( ghttp.RespondWith(http.StatusOK, expectedBody), ), ) root, err := client.Root() Expect(err).NotTo(HaveOccurred()) Expect(root).To(Equal(expectedRoot)) }) }) Context("when creating request fails with error", func() { BeforeEach(func() { client = oauth.NewClient("", "", "", testLogger) }) It("forwards the error", func() { _, err := client.Root() Expect(err).To(HaveOccurred()) }) }) Context("when executing request fails with error", func() { BeforeEach(func() { client = oauth.NewClient("", "", "http://not-a-real-url.com", testLogger) }) It("forwards the error", func() {
var ( client wl.Client server *ghttp.Server apiURL string testLogger logger.Logger ) func TestWL(t *testing.T) { RegisterFailHandler(Fail) RunSpecs(t, "WL Suite") } var _ = BeforeEach(func() { server = ghttp.NewServer() apiURL = server.URL() testLogger = logger.NewTestLogger(GinkgoWriter) client = oauth.NewClient( dummyAccessToken, dummyClientID, apiURL, testLogger, ) }) var _ = AfterEach(func() { server.Close() })
} if wlClientID == "" { Fail(fmt.Sprintf("Error - %s must be provided", wlClientIDEnvKey)) } By("Compiling binary") var err error wlBinPath, err = gexec.Build("github.com/robdimsdale/wl/cmd/wl", "-race") Expect(err).ShouldNot(HaveOccurred()) By("Creating client") testLogger := logger.NewTestLogger(GinkgoWriter) client = oauth.NewClient( wlAccessToken, wlClientID, apiURL, testLogger, ) By("Logging in") Eventually(func() error { _, err := client.User() return err }).Should(Succeed()) }) var _ = AfterSuite(func() { gexec.CleanupBuildArtifacts() })