示例#1
0
文件: user_test.go 项目: sluu99/um
func TestSetEmailAddr(t *testing.T) {
	session := testSetup()
	defer testTearDown(session)
	manager, err := um.Open("postgres", c_testDns)
	if err != nil {
		panic(err)
	}
	defer manager.Close()

	user, err := manager.FindById(1)
	if err != nil {
		panic(err)
	}

	err = user.SetEmailAddr("*****@*****.**")
	if err != nil {
		panic(err)
	}
	if emailAddr := user.EmailAddr(); emailAddr != "*****@*****.**" {
		t.Errorf("Email address returns '%s', but '*****@*****.**' is expected", emailAddr)
		t.Fail()
	}
	if !assertRecord(session, "um_users", map[string]interface{}{"id": 1, "email_addr": "*****@*****.**"}) {
		t.Error("Email address is not updated in the database")
		t.Fail()
	}
}
示例#2
0
文件: user_test.go 项目: sluu99/um
func TestSetStatus(t *testing.T) {
	session := testSetup()
	defer testTearDown(session)
	manager, err := um.Open("postgres", c_testDns)
	if err != nil {
		panic(err)
	}
	defer manager.Close()

	user, err := manager.FindById(1)
	if err != nil {
		panic(err)
	}

	status := int32(rand.Int63())

	err = user.SetStatus(status)
	if err != nil {
		panic(err)
	}
	if newStatus := user.Status(); newStatus != status {
		t.Errorf("Status returns '%d', but '%d' is expected", newStatus, status)
		t.Fail()
	}
	if !assertRecord(session, "um_users", map[string]interface{}{"id": 1, "status": status}) {
		t.Error("Email address is not updated in the database")
		t.Fail()
	}
}
示例#3
0
文件: manager_test.go 项目: sluu99/um
// TestEmailExists makes sure EmailExists returns true iff there's a user record in the database
func TestEmailExists(t *testing.T) {
	session := testSetup()
	defer testTearDown(session)
	manager, err := um.Open("postgres", c_testDns)
	if err != nil {
		panic(err)
	}
	defer manager.Close()

	exists, err := manager.EmailAddrExists("*****@*****.**")
	if err != nil {
		panic(err)
	}
	if exists != true {
		t.Error("fixtureuser1 exists in the database")
	}
	exists, err = manager.EmailAddrExists("*****@*****.**")
	if err != nil {
		panic(err)
	}
	if exists != true {
		t.Error("FIXTUREUSER2 (case insensitive) exists in the database")
	}
	exists, err = manager.EmailAddrExists("*****@*****.**")
	if err != nil {
		panic(err)
	}
	if exists != false {
		t.Error("fixtureuser3doesnotexist does not exist in the database")
	}
}
示例#4
0
文件: user_test.go 项目: sluu99/um
func TestSetDisplayName(t *testing.T) {
	session := testSetup()
	defer testTearDown(session)
	manager, err := um.Open("postgres", c_testDns)
	if err != nil {
		panic(err)
	}
	defer manager.Close()

	user, err := manager.FindById(1)
	if err != nil {
		panic(err)
	}

	err = user.SetDisplayName("A New Name")
	if err != nil {
		panic(err)
	}
	if name := user.DisplayName(); name != "A New Name" {
		t.Errorf("Display name returns '%s', but 'A New Name' is expected", name)
		t.Fail()
	}
	if !assertRecord(session, "um_users", map[string]interface{}{"id": 1, "display_name": "A New Name"}) {
		t.Error("Display name is not updated in the database")
		t.Fail()
	}
}
示例#5
0
文件: manager_test.go 项目: sluu99/um
func TestAuthenticateUpdateLogin(t *testing.T) {
	session := testSetup()
	defer testTearDown(session)
	manager, err := um.Open("postgres", c_testDns)
	if err != nil {
		panic(err)
	}
	defer manager.Close()

	user, err := manager.FindById(1)
	if err != nil {
		panic(err)
	}
	lastLogin := user.LastLogin()
	time.Sleep(3 * time.Second) // make sure some time has passed
	if manager.Authenticate(user, []byte("Password123"), true) != nil {
		t.Error("Authenticate must return nil when the password matches")
		t.FailNow()
	}
	if !user.LastLogin().After(lastLogin) {
		t.Error("Successful authentication must update the User last login when flag is set to true")
		t.Fail()
	}
	if !assertRecord(session, "um_users", map[string]interface{}{"id": user.Id(), "last_login": user.LastLogin()}) {
		t.Error("Successful authentication must update last login within the database when flag is set to true")
		t.Fail()
	}
}
示例#6
0
文件: manager_test.go 项目: sluu99/um
// TestFindByIdSuccess makes sure FindById returns the correct existing user
func TestFindByIdSuccess(t *testing.T) {
	session := testSetup()
	defer testTearDown(session)
	manager, err := um.Open("postgres", c_testDns)
	if err != nil {
		panic(err)
	}
	defer manager.Close()

	user, err := manager.FindById(1)
	if err != nil {
		panic(err)
	}
	if user == nil {
		t.Error("FindById did not return a valid user for ID #1")
		t.FailNow()
	}
	if id := user.Id(); id != 1 {
		t.Errorf("User ID returns %d, but 1 is expected", id)
		t.Fail()
	}
	if emailAddr := user.EmailAddr(); emailAddr != "*****@*****.**" {
		t.Errorf("Email address returns '%s', but '*****@*****.**' is expected", emailAddr)
		t.Fail()
	}
	if displayName := user.DisplayName(); displayName != "Fixture User 1" {
		t.Errorf("Display name is '%s' but 'Fixture User 1' is expected", displayName)
		t.Fail()
	}
}
示例#7
0
文件: manager_test.go 项目: sluu99/um
// TestFindSuccess makes sure Find returns a valid User when user exists
func TestFindSuccess(t *testing.T) {
	session := testSetup()
	defer testTearDown(session)
	manager, err := um.Open("postgres", c_testDns)
	if err != nil {
		panic(err)
	}
	defer manager.Close()

	testFindSuccessHelper(t, manager, "*****@*****.**", 1, "Fixture User 1")
	testFindSuccessHelper(t, manager, "*****@*****.**", 2, "Fixture User 2")
}
示例#8
0
文件: manager_test.go 项目: sluu99/um
func TestCreateInvalidUser(t *testing.T) {
	session := testSetup()
	defer testTearDown(session)
	manager, err := um.Open("postgres", c_testDns)
	if err != nil {
		panic(err)
	}
	defer manager.Close()

	user, err := manager.CreateUser("  ", "Some User", 0)
	if user != nil || err == nil {
		t.Error("CreateUser must return nil and an error when the email address is empty")
	}
}
示例#9
0
文件: manager_test.go 项目: sluu99/um
// TestFindFailture makes sure Find returns nil & error when user doesn't exist
func TestFindFailure(t *testing.T) {
	session := testSetup()
	defer testTearDown(session)
	manager, err := um.Open("postgres", c_testDns)
	if err != nil {
		panic(err)
	}
	defer manager.Close()

	user, err := manager.Find("userdoesnotexist")
	if user != nil || err == nil {
		t.Error("Find must return nil and an error when user does not exist")
		t.FailNow()
	}
}
示例#10
0
文件: manager_test.go 项目: sluu99/um
// TestFindByIdFail makes sure FindById returns nil and an error when the user does not exist
func TestFindByIdFail(t *testing.T) {
	session := testSetup()
	defer testTearDown(session)
	manager, err := um.Open("postgres", c_testDns)
	if err != nil {
		panic(err)
	}
	defer manager.Close()

	user, err := manager.FindById(1000)
	if !(err != nil && user == nil) {
		t.Error("FindById must return an error and a nil-um.User")
		t.FailNow()
	}
}
示例#11
0
文件: manager_test.go 项目: sluu99/um
// TestCreateValidUser makes sure a the user manager can handle a valid user creation
func TestCreateValidUser(t *testing.T) {
	session := testSetup()
	defer testTearDown(session)

	manager, err := um.Open("postgres", c_testDns)
	if err != nil {
		panic(err)
	}
	defer manager.Close()

	status := rand.Int31()
	user, err := manager.CreateUser("*****@*****.**", "Test User", status)
	if err != nil {
		panic(err)
	}
	if user == nil {
		t.Error("CreateUser did not return a valid User structure")
		t.FailNow()
	}
	if user.Id() == 0 {
		t.Error("CreateUser did not give the User structure a valid ID number")
		t.Fail()
	}
	if user.EmailAddr() != "*****@*****.**" {
		t.Errorf("EmailAddr is '%s' instead of '*****@*****.**'", user.EmailAddr())
		t.Fail()
	}
	if displayName := user.DisplayName(); displayName != "Test User" {
		t.Errorf("Display name is '%s' but 'Test User' is expected", displayName)
		t.Fail()
	}
	if user.Status() != status {
		t.Errorf("Status is '%d' instead of '%d' as expected", user.Status(), status)
		t.Fail()
	}

	props := map[string]interface{}{
		"id":           user.Id(),
		"email_addr":   user.EmailAddr(),
		"status":       user.Status(),
		"display_name": user.DisplayName(),
	}
	if !assertRecord(session, "um_users", props) {
		t.Error("Cannot find the coresponding user record in the database")
		t.FailNow()
	}
}
示例#12
0
文件: manager_test.go 项目: sluu99/um
func TestAuthenticateFail(t *testing.T) {
	session := testSetup()
	defer testTearDown(session)
	manager, err := um.Open("postgres", c_testDns)
	if err != nil {
		panic(err)
	}
	defer manager.Close()

	user, err := manager.FindById(1)
	if err != nil {
		panic(err)
	}
	if manager.Authenticate(user, []byte("WrongPassword"), false) == nil {
		t.Error("Authenticate must return an error when password does not match")
		t.FailNow()
	}
}
示例#13
0
文件: manager_test.go 项目: sluu99/um
// TestAuthenticateSuccess makes sure authenticate returns nil
// when the password matches
func TestAuthenticateSuccess(t *testing.T) {
	session := testSetup()
	defer testTearDown(session)
	manager, err := um.Open("postgres", c_testDns)
	if err != nil {
		panic(err)
	}
	defer manager.Close()

	user, err := manager.FindById(1)
	if err != nil {
		panic(err)
	}
	if manager.Authenticate(user, []byte("Password123"), false) != nil {
		t.Error("Authenticate must return nil when the password matches")
		t.FailNow()
	}
}
示例#14
0
文件: user_test.go 项目: sluu99/um
func TestSetPassword(t *testing.T) {
	session := testSetup()
	defer testTearDown(session)
	manager, err := um.Open("postgres", c_testDns)
	if err != nil {
		panic(err)
	}
	defer manager.Close()

	user, err := manager.FindById(1)
	if err != nil {
		panic(err)
	}

	oldHash := user.Hash()
	oldSalt := user.Salt()
	err = user.SetPassword([]byte("NewPassword1234"))
	if err != nil {
		panic(err)
	}
	err = manager.Authenticate(user, []byte("NewPassword1234"), false)
	if err != nil {
		t.Error("Fail to authenticate with the new password")
		t.Fail()
	}
	if hash := user.Hash(); bytes.Equal(hash, oldHash) {
		t.Error("SetPassword did not modify hash")
		t.Fail()
	}
	if salt := user.Salt(); bytes.Equal(salt, oldSalt) {
		t.Error("SetPassword did not generate a new salt")
		t.Fail()
	}
	if !assertRecord(session, "um_users", map[string]interface{}{"id": 1, "hash": fmt.Sprintf("%02x", user.Hash()), "salt": fmt.Sprintf("%02x", user.Salt())}) {
		t.Error("SetPassword did not update the database")
		t.Fail()
	}
}