コード例 #1
0
ファイル: auth.go プロジェクト: algoadv/etcd
func (u User) CheckPassword(password string) bool {
	err := bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password))
	return err == nil
}
コード例 #2
0
ファイル: auth_test.go プロジェクト: algoadv/etcd
func TestMergeUser(t *testing.T) {
	tbl := []struct {
		input  User
		merge  User
		expect User
		iserr  bool
	}{
		{
			User{User: "******"},
			User{User: "******"},
			User{},
			true,
		},
		{
			User{User: "******"},
			User{User: "******"},
			User{User: "******", Roles: []string{}},
			false,
		},
		{
			User{User: "******"},
			User{User: "******", Grant: []string{"role1"}},
			User{User: "******", Roles: []string{"role1"}},
			false,
		},
		{
			User{User: "******", Roles: []string{"role1"}},
			User{User: "******", Grant: []string{"role1"}},
			User{},
			true,
		},
		{
			User{User: "******", Roles: []string{"role1"}},
			User{User: "******", Revoke: []string{"role2"}},
			User{},
			true,
		},
		{
			User{User: "******", Roles: []string{"role1"}},
			User{User: "******", Grant: []string{"role2"}},
			User{User: "******", Roles: []string{"role1", "role2"}},
			false,
		},
		{
			User{User: "******"},
			User{User: "******", Password: "******"},
			User{User: "******", Roles: []string{}, Password: "******"},
			false,
		},
	}

	for i, tt := range tbl {
		out, err := tt.input.merge(tt.merge)
		if err != nil && !tt.iserr {
			t.Fatalf("Got unexpected error on item %d", i)
		}
		if !tt.iserr {
			err := bcrypt.CompareHashAndPassword([]byte(out.Password), []byte(tt.merge.Password))
			if err == nil {
				tt.expect.Password = out.Password
			}
			if !reflect.DeepEqual(out, tt.expect) {
				t.Errorf("Unequal merge expectation on item %d: got: %#v, expect: %#v", i, out, tt.expect)
			}
		}
	}
}