func putCorrectDataUser(user *models.User) { date := time.Now() user.IdUser = "******" user.Name = "Juan Login" user.Pass = "******" user.Email = "*****@*****.**" user.LastLogin = date.Add(time.Minute * (-5)) user.CreatedAt = date.AddDate(-1, 0, 0) user.UpdateAt = date.Add(time.Hour * (-5)) token, err := models.GenerateToken(user.Name, user.Pass) user.Token = token err = user.Save() if err != nil { log.Println("Can't save user for test login", err) } }
body, _ := json.Marshal(user) request, _ = http.NewRequest("POST", "/auth/login", bytes.NewReader(body)) }) It("Response should be 400 with empty input", func() { serve.Handler.ServeHTTP(recorder, request) Expect(recorder.Code).To(gomega.Equal(400)) }) It("Response should be 400 with wrong data", func() { user := models.User{} putCorrectDataUser(&user) user.Name = "wrong" user.IdUser = "******" user.Pass = "******" body, _ := json.Marshal(user) request, _ = http.NewRequest("POST", "/auth/login", bytes.NewReader(body)) serve.Handler.ServeHTTP(recorder, request) Expect(recorder.Code).To(gomega.Equal(404)) }) }) Context("With valid JSON", func() { var user models.User user = models.User{} putCorrectDataUser(&user)
BeforeEach(func() { user = gory.Build("userOk").(*models.User) token, _ := models.GenerateToken(user.Email + "#" + user.Pass) user.Token = token user.Save() body := "" request, _ = http.NewRequest("GET", "/fake", strings.NewReader(body)) request.Header.Add("Authorization", "Bearer "+token.Hash) }) It("Check if user with token is saved on the database", func() { chkUser := models.User{} chkUser.IdUser = user.IdUser chkUser, err := chkUser.Get() Expect(err).To(gomega.BeNil()) Expect(chkUser).ShouldNot(gomega.BeZero()) }) It("Return status code 200", func() { serve.Handler.ServeHTTP(recorder, request) Expect(recorder.Code).To(gomega.Equal(200)) }) }) Context("With a user who does not exist in the database", func() { var user *models.User