github.com/marinho/drone@v0.2.1-0.20140504195434-d3ba962e89a7/pkg/database/testing/users_test.go (about) 1 package database 2 3 import ( 4 "testing" 5 6 "github.com/drone/drone/pkg/database" 7 ) 8 9 // TODO unit test to verify unique constraint on User.Username 10 // TODO unit test to verify unique constraint on User.Email 11 12 // TestGetUser tests the ability to retrieve a User 13 // from the database by Unique ID. 14 func TestGetUser(t *testing.T) { 15 Setup() 16 defer Teardown() 17 18 u, err := database.GetUser(1) 19 if err != nil { 20 t.Error(err) 21 } 22 23 if u.ID != 1 { 24 t.Errorf("Exepected ID %d, got %d", 1, u.ID) 25 } 26 27 if u.Password != "$2a$10$b8d63QsTL38vx7lj0HEHfOdbu1PCAg6Gfca74UavkXooIBx9YxopS" { 28 t.Errorf("Exepected Password %s, got %s", "$2a$10$b8d63QsTL38vx7lj0HEHfOdbu1PCAg6Gfca74UavkXooIBx9YxopS", u.Password) 29 } 30 31 if u.Token != "123" { 32 t.Errorf("Exepected Token %s, got %s", "123", u.Token) 33 } 34 35 if u.Name != "Brad Rydzewski" { 36 t.Errorf("Exepected Name %s, got %s", "Brad Rydzewski", u.Name) 37 } 38 39 if u.Email != "brad.rydzewski@gmail.com" { 40 t.Errorf("Exepected Email %s, got %s", "brad.rydzewski@gmail.com", u.Email) 41 } 42 43 if u.Gravatar != "8c58a0be77ee441bb8f8595b7f1b4e87" { 44 t.Errorf("Exepected Gravatar %s, got %s", "8c58a0be77ee441bb8f8595b7f1b4e87", u.Gravatar) 45 } 46 } 47 48 // TestGetUseEmail tests the ability to retrieve a User 49 // from the database by Email address. 50 func TestGetUserEmail(t *testing.T) { 51 Setup() 52 defer Teardown() 53 54 u, err := database.GetUserEmail("brad.rydzewski@gmail.com") 55 if err != nil { 56 t.Error(err) 57 } 58 59 if u.ID != 1 { 60 t.Errorf("Exepected ID %d, got %d", 1, u.ID) 61 } 62 63 if u.Password != "$2a$10$b8d63QsTL38vx7lj0HEHfOdbu1PCAg6Gfca74UavkXooIBx9YxopS" { 64 t.Errorf("Exepected Password %s, got %s", "$2a$10$b8d63QsTL38vx7lj0HEHfOdbu1PCAg6Gfca74UavkXooIBx9YxopS", u.Password) 65 } 66 67 if u.Token != "123" { 68 t.Errorf("Exepected Token %s, got %s", "123", u.Token) 69 } 70 71 if u.Name != "Brad Rydzewski" { 72 t.Errorf("Exepected Name %s, got %s", "Brad Rydzewski", u.Name) 73 } 74 75 if u.Email != "brad.rydzewski@gmail.com" { 76 t.Errorf("Exepected Email %s, got %s", "brad.rydzewski@gmail.com", u.Email) 77 } 78 79 if u.Gravatar != "8c58a0be77ee441bb8f8595b7f1b4e87" { 80 t.Errorf("Exepected Gravatar %s, got %s", "8c58a0be77ee441bb8f8595b7f1b4e87", u.Gravatar) 81 } 82 } 83 84 // TestUpdateUser tests the ability to updatee an 85 // existing User in the database. 86 func TestUpdateUser(t *testing.T) { 87 Setup() 88 defer Teardown() 89 90 // get the user we plan to update 91 user, err := database.GetUser(1) 92 if err != nil { 93 t.Error(err) 94 } 95 96 // update fields 97 user.Email = "brad@drone.io" 98 user.Password = "password" 99 100 // update the database 101 if err := database.SaveUser(user); err != nil { 102 t.Error(err) 103 } 104 105 // get the updated user 106 updatedUser, err := database.GetUser(1) 107 if err != nil { 108 t.Error(err) 109 } 110 111 // verify the updated fields 112 if user.Email != updatedUser.Email { 113 t.Errorf("Exepected Email %s, got %s", user.Email, updatedUser.Email) 114 } 115 116 if user.Password != updatedUser.Password { 117 t.Errorf("Exepected Password %s, got %s", user.Email, updatedUser.Password) 118 } 119 } 120 121 // Deletes an existing User account. 122 func TestDeleteUser(t *testing.T) { 123 Setup() 124 defer Teardown() 125 126 // get the user we plan to update 127 if err := database.DeleteUser(1); err != nil { 128 t.Error(err) 129 } 130 131 // now try to get the user from the database 132 _, err := database.GetUser(1) 133 if err == nil { 134 t.Fail() 135 } 136 } 137 138 // Returns a list of all Users. 139 func TestListUsers(t *testing.T) { 140 Setup() 141 defer Teardown() 142 143 users, err := database.ListUsers() 144 if err != nil { 145 t.Error(err) 146 } 147 148 // verify user count 149 if len(users) != 3 { 150 t.Errorf("Exepected %d users in database, got %d", 3, len(users)) 151 return 152 } 153 154 // get the first user in the list and verify 155 // fields are being populated correctly 156 u := users[0] 157 158 if u.ID != 1 { 159 t.Errorf("Exepected ID %d, got %d", 1, u.ID) 160 } 161 162 if u.Password != "$2a$10$b8d63QsTL38vx7lj0HEHfOdbu1PCAg6Gfca74UavkXooIBx9YxopS" { 163 t.Errorf("Exepected Password %s, got %s", "$2a$10$b8d63QsTL38vx7lj0HEHfOdbu1PCAg6Gfca74UavkXooIBx9YxopS", u.Password) 164 } 165 166 if u.Token != "123" { 167 t.Errorf("Exepected Token %s, got %s", "123", u.Token) 168 } 169 170 if u.Name != "Brad Rydzewski" { 171 t.Errorf("Exepected Name %s, got %s", "Brad Rydzewski", u.Name) 172 } 173 174 if u.Email != "brad.rydzewski@gmail.com" { 175 t.Errorf("Exepected Email %s, got %s", "brad.rydzewski@gmail.com", u.Email) 176 } 177 178 if u.Gravatar != "8c58a0be77ee441bb8f8595b7f1b4e87" { 179 t.Errorf("Exepected Gravatar %s, got %s", "8c58a0be77ee441bb8f8595b7f1b4e87", u.Gravatar) 180 } 181 }