Beispiel #1
0
func TestChangePassword(t *testing.T) {
	user := vsafe.User{Id: 1}
	var err error
	if err = user.Init("keep94", "somepassword"); err != nil {
		t.Fatalf("Error creating user: %v", err)
	}
	if err = user.ChangePassword("somepassword", "another"); err != nil {
		t.Fatalf("Verify password failed: %v", err)
	}
	if _, err = user.VerifyPassword("another"); err != nil {
		t.Error("Expected password to change")
	}
	if err = user.ChangePassword("somepassword", "be"); err != vsafe.ErrWrongPassword {
		t.Errorf("Expected ErrWrongPassword, got %v", err)
	}
}
Beispiel #2
0
// ChangePassword changes the password of a user in persistent storage.
// t, the transaction, must be non nil.
func ChangePassword(
	store SafeUpdateUserRunner,
	t db.Transaction,
	id int64,
	oldPass, newPass string) (*vsafe.User, error) {
	if t == nil {
		panic("Transaction must be non-nil")
	}
	var user vsafe.User
	err := store.UserById(t, id, &user)
	if err != nil {
		return nil, err
	}
	if err = user.ChangePassword(oldPass, newPass); err != nil {
		return nil, err
	}
	if err = store.UpdateUser(t, &user); err != nil {
		return nil, err
	}
	return &user, nil
}