Exemplo n.º 1
0
func TestAuthen(t *testing.T) {
	database.ClearAllUser()
	Convey("POST Login", t, func() {
		Convey("Register new account must successful!", func() {
			user := CloneUserModel(userValidData)
			response := do_request("POST", userApi, user)
			body := parse_response(response)
			var responseUser models.User
			err := json.Unmarshal(body, &responseUser)
			So(err, ShouldBeNil)
			So(response.StatusCode, ShouldEqual, 201)

			Convey("Login with correct account should successful!", func() {
				user := CloneUserModel(userValidData)
				response := do_request("POST", authApi+"login", user)
				body := parse_response(response)
				var loginSuccess LoginResponse
				err := json.Unmarshal(body, &loginSuccess)
				So(err, ShouldBeNil)
				So(response.StatusCode, ShouldEqual, 200)
				So(loginSuccess.Email, ShouldEqual, user.Email)
				So(loginSuccess.ApiKey, ShouldNotBeNil)
				Println(loginSuccess.Password)
			})
		})
		Convey("Login with not have accout should fail", func() {
			user := CloneUserModel(userValidData)
			user.Email = "*****@*****.**"
			response := do_request("POST", authApi+"login", user)
			body := parse_response(response)

			var loginFail Error
			err := json.Unmarshal(body, &loginFail)
			So(err, ShouldBeNil)
		})

	})
}
Exemplo n.º 2
0
func TestUser(t *testing.T) {

	database.ClearAllUser()

	Convey("GET user", t, func() {
		Convey("Get not exist user shoud return not found error", func() {
			So(1, ShouldEqual, 1)
		})
	})

	Convey("POST create user", t, func() {

		Convey("Create not exist user should response status 201 and correct user data.", func() {
			user := CloneUserModel(userValidData)
			response := do_request("POST", userApi, user)
			body := parse_response(response)
			var responseUser userModel
			err := json.Unmarshal(body, &responseUser)
			So(err, ShouldBeNil)
			So(response.StatusCode, ShouldEqual, 201)
			So(responseUser.Email, ShouldEqual, user.Email)
			So(responseUser.Name, ShouldEqual, user.Name)
			So(responseUser.Role, ShouldEqual, 0)

			var userInDb userModel
			database.Collection(UserColName).FindId(responseUser.Id).One(&userInDb)
			So(userInDb.Id, ShouldEqual, responseUser.Id)

			Convey("Create exist user should response status 400 and exist message", func() {
				response := do_request("POST", userApi, userValidData)
				body := parse_response(response)
				var responseError Error
				err := json.Unmarshal(body, &responseError)
				So(err, ShouldBeNil)
				So(response.StatusCode, ShouldEqual, 400)
				So(responseError.Id, ShouldEqual, "USER_EXIST")
				So(responseError.Message, ShouldEqual, "This user has been exist!")
			})
		})

		Convey("Create with invalid email should return status 400 and email invalid message", func() {
			response := do_request("POST", userApi, userInvalidEmailData)
			body := parse_response(response)
			var responseData Error
			err := json.Unmarshal(body, &responseData)
			So(err, ShouldBeNil)
			So(response.StatusCode, ShouldEqual, 400)
			So(responseData.Id, ShouldEqual, "USER_EMAIL_INVALID")
			So(responseData.Message, ShouldEqual, "Email invalid")
		})
		Convey("Create with empty email should return status 400 and email required message", func() {
			response := do_request("POST", userApi, userInvalidEmailEmptyData)
			body := parse_response(response)
			var responseData Error
			err := json.Unmarshal(body, &responseData)
			So(err, ShouldBeNil)
			So(response.StatusCode, ShouldEqual, 400)
			So(responseData.Id, ShouldEqual, "USER_EMAIL_REQUIRED")
			So(responseData.Message, ShouldEqual, "Email is required")
		})
		Convey("Create with empty password should return status 400 and password required message", func() {
			response := do_request("POST", userApi, userInvalidPasswordEmptyData)
			body := parse_response(response)
			var responseData Error
			err := json.Unmarshal(body, &responseData)
			So(err, ShouldBeNil)
			So(response.StatusCode, ShouldEqual, 400)
			So(responseData.Id, ShouldEqual, "USER_PASSWORD_REQUIRED")
			So(responseData.Message, ShouldEqual, "Password is required")
		})
	})
}