Exemple #1
0
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)
	}
}
Exemple #2
0
func RegisterHandler(w http.ResponseWriter, r *http.Request) {

	// We obtain user data, and create our user with them
	rd := render.New()
	user, err := DecodeUserData(r.Body)
	if err != nil {
		log.Println("Unmarshal json register error", err)
		rd.JSON(w, http.StatusBadRequest, map[string]string{"token": ""})
		return
	}

	if !user.Valid() {
		log.Println("User data are invalid!")
		rd.JSON(w, http.StatusBadRequest, map[string]string{"token": ""})
		return
	}

	token, err := models.GenerateToken(user.Name, "#", user.Pass)
	if err != nil {
		log.Println("Can't create token!", err)
		rd.JSON(w, http.StatusBadRequest, map[string]string{"token": ""})
		return
	}

	user.Token = token
	// keep user data and token we created in the database
	err = user.Save()
	if err != nil {
		log.Println("Can't save the user on the database!")
		rd.JSON(w, http.StatusBadRequest, map[string]string{"token": ""})
		return
	}

	// if everything went correctly, we send the token to the client
	rd.JSON(w, http.StatusOK, map[string]string{"token": user.Token.Hash})
}
Exemple #3
0
		var email string
		var pass string
		var data string
		var token models.Token
		var err error

		Describe("Generating token", func() {

			Context("With valid data", func() {

				BeforeEach(func() {
					email = "*****@*****.**"
					pass = "******"
					data = email + pass
					token, err = models.GenerateToken(data)
				})

				It("Generate token", func() {
					Expect(err).To(gomega.BeNil())
					Expect(token).ShouldNot(gomega.BeZero())
				})

				It("Create correct size", func() {
					n := strings.SplitN(token.Hash, ".", 3)
					Expect(len(n)).To(gomega.Equal(3))
				})
			})

			Context("With invalid data", func() {
Exemple #4
0
				BeforeEach(func() {
					body := ""
					request, _ = http.NewRequest("GET", "/fake",
						strings.NewReader(body))
				})

				It("Return status code 400", func() {
					serve.Handler.ServeHTTP(recorder, request)
					Expect(recorder.Code).To(gomega.Equal(400))
				})
			})

			Context("With authorization header but a user who doesn't exist in the database", func() {

				BeforeEach(func() {
					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