Пример #1
0
func TestSaveValidUser(t *testing.T) {
	defer testhelpers.ClearDb()

	u := User{
		Name:     "admin",
		Email:    "*****@*****.**",
		Password: "******",
	}

	u2, err := u.Save()
	if err != nil {
		t.Error("Expected valid user to be saved successfully, save failed with error", err)
	}

	if u2 == nil {
		t.Error("Expected saved user to be found in database, not found.")
	}

	if u2.Name != u.Name {
		t.Error("Expected saved user to have name ", u.Name, "got ", u2.Name)
	}

	if u2.Email != u.Email {
		t.Error("Expected saved user to have email ", u.Email, "got ", u2.Email)
	}

	if u2.Password != "" {
		t.Error("Expected the database to NOT store raw password, it was stored in the database.")
	}
}
Пример #2
0
// Test valid and invalid authentication
func TestAuthenticate(t *testing.T) {
	defer testhelpers.ClearDb()
	authBackend, _ := InitJwtAuthBackend()

	// First, create a dummy user
	dummyu := user.DummyUser()
	dummyu.Save()

	u := user.DummyUser()

	// Test valid authentication
	loggedin := authBackend.Authenticate(&u)
	if !loggedin {
		t.Error("Expected right user credentials to be valid, got invalid.")
	}

	// Test invalid authentication
	u2 := user.User{
		Name:     "Marc",
		Email:    "*****@*****.**",
		Password: "******",
	}
	loggedin = authBackend.Authenticate(&u2)
	if loggedin {
		t.Error("Expected wrong user authentication to be invalid, go valid.")
	}
}
Пример #3
0
func TestDeleteUser(t *testing.T) {
	defer testhelpers.ClearDb()
	u := DummyUser()

	// call user delete now should return an err not found
	// since the user is not added to the database yes
	if err := u.Delete(); err == nil {
		t.Error(
			"For non-existing user",
			"expect error",
			"got no error",
		)
	}

	u2, _ := u.Save()
	if err := u2.Delete(); err != nil {
		t.Error(
			"For existing user",
			"expect no error",
			"got error", err,
		)
	}

	if u3, _ := FindUser(neoism.Props{"name": u.Name}); u3 != nil {
		t.Error(
			"After deleting user",
			"expect FindUser to return nil",
			"got user Id", u3.Id,
		)
	}
}
Пример #4
0
func TestSaveInvalidUser(t *testing.T) {
	defer testhelpers.ClearDb()

	cases := []map[string]User{
		{
			"missing name": User{
				Email:    "*****@*****.**",
				Password: "******",
			},
		},
		{
			"missing email": User{
				Name:     "admin",
				Password: "******",
			},
		},
		{
			"missing password": User{
				Name:  "admin",
				Email: "*****@*****.**",
			},
		},
		{
			"name \"a admin\"": User{
				Name:     "a dmin",
				Email:    "*****@*****.**",
				Password: "******",
			},
		},
		{
			"email \"admin\"": User{
				Name:     "admin",
				Email:    "admin",
				Password: "******",
			},
		},
		{
			"email \"admin@admin\"": User{
				Name:     "admin",
				Email:    "admin@admin",
				Password: "******",
			},
		},
	}

	for _, c := range cases {
		for k, u := range c {
			if u2, err := u.Save(); err == nil {
				t.Error(
					"For user with", k,
					"expected u.Save() to fail",
					"got user with  Id", u2.Id, "created")
			}
		}
	}
}
Пример #5
0
// Make sure generated token string at least has the correct format:
// xxx.xxx.xxx
func TestTokenGeneration(t *testing.T) {
	defer testhelpers.ClearDb()
	authBackend, _ := InitJwtAuthBackend()

	// First, create a dummy user
	dummyu := user.DummyUser()
	u, _ := dummyu.Save()

	token, err := authBackend.GenerateToken(u)

	if err != nil {
		t.Error(
			"When token data is valid,",
			"Expected token generation to be valid",
			"got error", err,
		)
	}

	if len(strings.Split(token, ".")) != 3 {
		t.Error("Wrong token format was generated.")
	}
}