This repository has been archived on 2022-08-17. You can view files and clone it, but cannot push or open issues or pull requests.
dex/functional/repo/password_repo_test.go

154 lines
3.1 KiB
Go
Raw Normal View History

2015-08-18 05:57:27 +05:30
package repo
import (
"os"
"testing"
"time"
2016-02-10 02:27:42 +05:30
"github.com/go-gorp/gorp"
2015-08-18 05:57:27 +05:30
"github.com/kylelemons/godebug/pretty"
"github.com/coreos/dex/db"
"github.com/coreos/dex/user"
)
var (
testPWs = []user.PasswordInfo{
{
UserID: "ID-1",
Password: []byte("hi."),
},
}
)
func newPasswordInfoRepo(t *testing.T) user.PasswordInfoRepo {
2016-02-10 02:27:42 +05:30
var dbMap *gorp.DbMap
if os.Getenv("DEX_TEST_DSN") == "" {
2016-02-10 02:27:42 +05:30
dbMap = db.NewMemDB()
} else {
dbMap = connect(t)
2015-08-18 05:57:27 +05:30
}
2016-02-10 02:27:42 +05:30
repo, err := db.NewPasswordInfoRepoFromPasswordInfos(dbMap, testPWs)
if err != nil {
t.Fatalf("Unable to add password infos: %v", err)
2015-08-18 05:57:27 +05:30
}
return repo
2015-08-18 05:57:27 +05:30
}
func TestCreatePasswordInfo(t *testing.T) {
tests := []struct {
pw user.PasswordInfo
err error
}{
{
pw: user.PasswordInfo{
UserID: "ID-2",
Password: user.Password("bob@example.com"),
},
err: nil,
},
{
pw: user.PasswordInfo{
UserID: "ID-3",
Password: user.Password("1234"),
PasswordExpires: time.Now().Round(time.Second).UTC(),
},
err: nil,
},
{
pw: user.PasswordInfo{
UserID: "ID-1",
Password: user.Password("1234"),
PasswordExpires: time.Now().Round(time.Second).UTC(),
},
err: user.ErrorDuplicateID,
},
{
pw: user.PasswordInfo{
Password: user.Password("1234"),
PasswordExpires: time.Now().Round(time.Second).UTC(),
},
err: user.ErrorInvalidID,
},
}
for i, tt := range tests {
repo := newPasswordInfoRepo(t)
2015-08-18 05:57:27 +05:30
err := repo.Create(nil, tt.pw)
if tt.err != nil {
if err != tt.err {
t.Errorf("case %d: want=%v, got=%v", i, tt.err, err)
}
} else {
if err != nil {
t.Errorf("case %d: want nil err, got %v", i, err)
}
gotPW, err := repo.Get(nil, tt.pw.UserID)
if err != nil {
t.Errorf("case %d: want nil err, got %v", i, err)
}
if diff := pretty.Compare(tt.pw, gotPW); diff != "" {
t.Errorf("case %d: Compare(want, got) = %v", i,
diff)
}
}
}
}
func TestUpdatePasswordInfo(t *testing.T) {
tests := []struct {
pw user.PasswordInfo
err error
}{
{
pw: user.PasswordInfo{
UserID: "ID-1",
Password: user.Password("new_pass"),
PasswordExpires: time.Now().Round(time.Second).UTC(),
},
err: nil,
},
{
pw: user.PasswordInfo{
UserID: "ID-2",
Password: user.Password("new_pass"),
PasswordExpires: time.Now().Round(time.Second).UTC(),
},
err: user.ErrorNotFound,
},
{
pw: user.PasswordInfo{
UserID: "ID-1",
PasswordExpires: time.Now().Round(time.Second).UTC(),
},
err: user.ErrorInvalidPassword,
},
}
for i, tt := range tests {
repo := newPasswordInfoRepo(t)
2015-08-18 05:57:27 +05:30
err := repo.Update(nil, tt.pw)
if tt.err != nil {
if err != tt.err {
t.Errorf("case %d: want=%q, got=%q", i, tt.err, err)
}
} else {
if err != nil {
t.Errorf("case %d: want nil err, got %q", i, err)
}
gotPW, err := repo.Get(nil, tt.pw.UserID)
if err != nil {
t.Errorf("case %d: want nil err, got %q", i, err)
}
if diff := pretty.Compare(tt.pw, gotPW); diff != "" {
t.Errorf("case %d: Compare(want, got) = %v", i,
diff)
}
}
}
}