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) } }
token, _ := models.GenerateToken("test") body := "" request, _ = http.NewRequest("GET", "/fake", strings.NewReader(body)) request.Header.Add("Authorization", "Bearer "+token.Hash) }) It("Return status code 400", func() { serve.Handler.ServeHTTP(recorder, request) Expect(recorder.Code).To(gomega.Equal(400)) }) }) Context("With a real user", func() { var user *models.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{}
Context("With invalid JSON", func() { BeforeEach(func() { body, _ := json.Marshal(gory.Build("userBad")) request, _ = http.NewRequest("POST", "/auth/signup", bytes.NewReader(body)) }) It("Return a status code of 400", func() { serve.Handler.ServeHTTP(recorder, request) Expect(recorder.Code).To(gomega.Equal(400)) }) It("User must not be saved in the database", func() { user := gory.Build("userBad").(*models.User) userTest := models.User{ IdUser: user.IdUser, } userTest, err := userTest.Get() errTest := errors.New("not found") Expect(err).To(gomega.Equal(errTest)) Expect(userTest).To(gomega.BeZero()) }) }) Context("With valid JSON", func() { BeforeEach(func() { body, _ := json.Marshal(gory.Build("userOk")) request, _ = http.NewRequest("POST", "/auth/signup", bytes.NewReader(body)) })
It := ginkgo.It Context := ginkgo.Context BeforeEach := ginkgo.BeforeEach BeforeEach(func() { models.DB_NAME = "server_test" }) Describe("Interacting with database", func() { It("Saving or update data", func() { user := models.User{ IdUser: "******", Name: "test", Pass: "******", Email: "*****@*****.**", LastLogin: time.Now(), CreatedAt: time.Now(), UpdateAt: time.Now(), } err := user.Save() Expect(err).To(gomega.BeNil()) }) It("Get user with Id", func() { user := models.User{ IdUser: "******", } user, err := user.Get()