Skip to content

Commit

Permalink
fix: user tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rmnvlv committed May 4, 2022
1 parent 7040125 commit 6c602e8
Showing 1 changed file with 101 additions and 32 deletions.
133 changes: 101 additions & 32 deletions internal/repository/postgres/user_postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import (
hash "borda/pkg/hash"
"testing"

"github.com/jmoiron/sqlx"
"github.com/stretchr/testify/require"
)

func TestUserRepository_SaveUser(t *testing.T) {
db := MustOpenDB(t)
defer MustCloseDB(t, db)

repo := postgres.NewUserRepository(db)
require := require.New(t)

Expand Down Expand Up @@ -50,6 +53,8 @@ func TestUserRepository_SaveUser(t *testing.T) {

func TestUserRepository_GetUserByCredentials(t *testing.T) {
db := MustOpenDB(t)
defer MustCloseDB(t, db)

repo := postgres.NewUserRepository(db)
require := require.New(t)

Expand All @@ -74,7 +79,7 @@ func TestUserRepository_GetUserByCredentials(t *testing.T) {
password: "User2Pass",
},
wantResponse: &domain.User{
Id: 2,
Id: 1,
Username: "User2",
Password: "User2Pass",
Contact: "@user2",
Expand All @@ -88,7 +93,7 @@ func TestUserRepository_GetUserByCredentials(t *testing.T) {
password: "User3Pass",
},
wantResponse: &domain.User{
Id: 3,
Id: 2,
Username: "User3",
Password: "User3Pass",
Contact: "@user3",
Expand All @@ -98,6 +103,7 @@ func TestUserRepository_GetUserByCredentials(t *testing.T) {
}
for _, testCase := range testTable {
t.Run(testCase.name, func(t *testing.T) {
helpCreateUser(t, db, testCase.wantResponse)

hashedPswd, err := hasher.Hash(testCase.args.password)
require.Equal(testCase.wantErr, err, t)
Expand All @@ -118,6 +124,8 @@ func TestUserRepository_GetUserByCredentials(t *testing.T) {

func TestUserRepository_GetUserById(t *testing.T) {
db := MustOpenDB(t)
defer MustCloseDB(t, db)

repo := postgres.NewUserRepository(db)
require := require.New(t)

Expand Down Expand Up @@ -149,19 +157,21 @@ func TestUserRepository_GetUserById(t *testing.T) {
{
name: "OK_2",
args: args{
id: 3,
id: 2,
},
wantResponse: &domain.User{
Id: 3,
Id: 2,
Username: "User3",
Password: "User3Pass",
Contact: "@user3",
},
wantErr: nil,
},
}

for _, testCase := range testTable {
t.Run(testCase.name, func(t *testing.T) {
helpCreateUser(t, db, testCase.wantResponse)

actualResponse, actualErr := repo.GetUserById(testCase.args.id)

Expand All @@ -179,50 +189,53 @@ func TestUserRepository_GetUserById(t *testing.T) {

func TestUserRepository_GetAllUsers(t *testing.T) {
db := MustOpenDB(t)
defer MustCloseDB(t, db)

repo := postgres.NewUserRepository(db)
require := require.New(t)

hasher := hash.NewSHA1Hasher(config.PasswordSalt())

testTable := []struct {
name string
want []*domain.User
wantErr error
name string
wantResponse []*domain.User
wantErr error
}{
// TODO: Add test cases.
{
name: "",
want: []*domain.User{{
Id: 1,
Username: "User1",
Password: "User1Pass",
Contact: "@user1",
TeamId: 1,
},
name: "OK",
wantResponse: []*domain.User{
{
Id: 1,
Username: "User1",
Password: "User1Pass",
Contact: "@user1",
TeamId: 0,
},
{
Id: 2,
Username: "User2",
Password: "User2Pass",
Contact: "@user2",
TeamId: 2,
TeamId: 0,
},
{
Id: 3,
Username: "User3",
Password: "User3Pass",
Contact: "@user3",
TeamId: 3,
}},
},
wantErr: nil,
},
}

for _, testCase := range testTable {
t.Run(testCase.name, func(t *testing.T) {
for _, user := range testCase.wantResponse {
helpCreateUser(t, db, user)
}

actualResponse, actualErr := repo.GetAllUsers()
require.Equal(testCase.wantErr, actualErr, t)

for i, user := range testCase.want {
i := 1

for _, user := range testCase.wantResponse {

wantPass, err := hasher.Hash(user.Password)
require.Equal(testCase.wantErr, err, t)
Expand All @@ -231,7 +244,8 @@ func TestUserRepository_GetAllUsers(t *testing.T) {
require.Equal(wantPass, actualResponse[i].Password, t)
require.Equal(user.Contact, actualResponse[i].Contact, t)
require.Equal(user.Username, actualResponse[i].Username, t)
require.Equal(user.TeamId, actualResponse[i].TeamId)
//require.Equal(user.TeamId, actualResponse[i].TeamId)
i--

}

Expand All @@ -249,6 +263,9 @@ func TestUserRepository_UpdatePassword(t *testing.T) {
type args struct {
userId int
newPassword string
username string
oldPassword string
contact string
}
testTable := []struct {
name string
Expand All @@ -261,6 +278,9 @@ func TestUserRepository_UpdatePassword(t *testing.T) {
args: args{
userId: 1,
newPassword: "NewPassword",
username: "User1",
oldPassword: "User1Pswd",
contact: "@user1",
},
wantErr: nil,
},
Expand All @@ -269,12 +289,24 @@ func TestUserRepository_UpdatePassword(t *testing.T) {
args: args{
userId: 2,
newPassword: "NewPswd",
username: "User2",
oldPassword: "User2Pswd",
contact: "@user2",
},
wantErr: nil,
},
}
for _, testCase := range testTable {
t.Run(testCase.name, func(t *testing.T) {

user := &domain.User{
Username: testCase.args.username,
Password: testCase.args.oldPassword,
Contact: testCase.args.contact,
}

helpCreateUser(t, db, user)

pswd, err := hasher.Hash(testCase.args.newPassword)
require.Equal(testCase.wantErr, err, t)

Expand All @@ -289,7 +321,7 @@ func TestUserRepository_UpdatePassword(t *testing.T) {
// db := MustOpenDB(t)
// repo := postgres.NewUserRepository(db)
// require := require.New(t)

//
// type args struct {
// userId int
// roleId int
Expand All @@ -312,7 +344,7 @@ func TestUserRepository_UpdatePassword(t *testing.T) {
// for _, testCase := range testTable {
// t.Run(testCase.name, func(t *testing.T) {
// TestUserRepository_SaveUser(t)

//
// actualErr := repo.AssignRole(testCase.args.userId, testCase.args.roleId)
// require.Equal(testCase.wantErr, actualErr, t)
// })
Expand All @@ -321,11 +353,16 @@ func TestUserRepository_UpdatePassword(t *testing.T) {

func TestUserRepository_GetUserRole(t *testing.T) {
db := MustOpenDB(t)
MustCloseDB(t, db)

repo := postgres.NewUserRepository(db)
require := require.New(t)

type args struct {
userId int
userId int
username string
password string
contact string
}
testTable := []struct {
name string
Expand All @@ -337,18 +374,24 @@ func TestUserRepository_GetUserRole(t *testing.T) {
{
name: "OK_1",
args: args{
userId: 1,
userId: 1,
username: "User1",
password: "User1Pswd",
contact: "@contact1",
},
wantResponse: &domain.Role{
Id: 1,
Name: "admin",
Id: 2,
Name: "user",
},
wantErr: nil,
},
{
name: "OK_2",
args: args{
userId: 2,
userId: 2,
username: "User2",
password: "User2Pswd",
contact: "@contact2",
},
wantResponse: &domain.Role{
Id: 2,
Expand All @@ -359,10 +402,36 @@ func TestUserRepository_GetUserRole(t *testing.T) {
}
for _, testCase := range testTable {
t.Run(testCase.name, func(t *testing.T) {
user := &domain.User{
Username: testCase.args.username,
Password: testCase.args.password,
Contact: testCase.args.contact,
}
helpCreateUser(t, db, user)

actualResponse, actualErr := repo.GetUserRole(testCase.args.userId)

require.Equal(testCase.wantErr, actualErr, t)
require.Equal(testCase.wantResponse, actualResponse, t)
})
}
}

func helpCreateUser(t *testing.T, db *sqlx.DB, user *domain.User) *domain.User {
t.Helper()

hasher := hash.NewSHA1Hasher(config.PasswordSalt())
hashedPswd, err := hasher.Hash(user.Password)
if err != nil {
t.Fatal(err)
}

id, err := postgres.NewUserRepository(db).SaveUser(user.Username, hashedPswd, user.Contact)
if err != nil {
t.Fatal(err)
}

user.Id = id

return user
}

0 comments on commit 6c602e8

Please sign in to comment.