github.com/bitcubate/cryptojournal@v1.2.5-0.20171102134152-f578b3d788ab/src/comments/actions/actions_test.go (about) 1 package commentactions 2 3 import ( 4 "fmt" 5 "net/http" 6 "net/http/httptest" 7 "net/url" 8 "strings" 9 "testing" 10 11 "github.com/fragmenta/mux" 12 "github.com/fragmenta/query" 13 14 "github.com/bitcubate/cryptojournal/src/comments" 15 "github.com/bitcubate/cryptojournal/src/lib/resource" 16 ) 17 18 // names is used to test setting and getting the first string field of the comment. 19 var names = []string{"foo", "bar"} 20 21 // testSetup performs setup for integration tests 22 // using the test database, real views, and mock authorisation 23 // If we can run this once for global tests it might be more efficient? 24 func TestSetup(t *testing.T) { 25 err := resource.SetupTestDatabase(3) 26 if err != nil { 27 fmt.Printf("comments: Setup db failed %s", err) 28 } 29 30 // Set up mock auth 31 resource.SetupAuthorisation() 32 33 // Load templates for rendering 34 resource.SetupView(3) 35 36 router := mux.New() 37 mux.SetDefault(router) 38 39 // FIXME - Need to write routes out here again, but without pkg prefix 40 // Any neat way to do this instead? We'd need a separate routes package under app... 41 router.Add("/comments", nil) 42 router.Add("/comments/create", nil) 43 router.Add("/comments/create", nil).Post() 44 router.Add("/comments/login", nil) 45 router.Add("/comments/login", nil).Post() 46 router.Add("/comments/login", nil).Post() 47 router.Add("/comments/logout", nil).Post() 48 router.Add("/comments/{id:\\d+}/update", nil) 49 router.Add("/comments/{id:\\d+}/update", nil).Post() 50 router.Add("/comments/{id:\\d+}/destroy", nil).Post() 51 router.Add("/comments/{id:\\d+}", nil) 52 53 // Delete all comments to ensure we get consistent results 54 query.ExecSQL("delete from comments;") 55 query.ExecSQL("ALTER SEQUENCE comments_id_seq RESTART WITH 1;") 56 57 // Delete all users to ensure we get consistent results? 58 _, err = query.ExecSQL("delete from users;") 59 if err != nil { 60 t.Fatalf("error setting up:%s", err) 61 } 62 // Insert a test admin user for checking logins - never delete as will 63 // be required for other resources testing 64 _, err = query.ExecSQL("INSERT INTO users (id,email,name,points,status,role,password_hash) VALUES(1,'example@example.com','admin',100,100,100,'$2a$10$2IUzpI/yH0Xc.qs9Z5UUL.3f9bqi0ThvbKs6Q91UOlyCEGY8hdBw6');") 65 if err != nil { 66 t.Fatalf("error setting up:%s", err) 67 } 68 // Insert user to delete 69 _, err = query.ExecSQL("INSERT INTO users (id,email,name,points,status,role,password_hash) VALUES(2,'example2@example.com','test',100,100,0,'$2a$10$2IUzpI/yH0Xc.qs9Z5UUL.3f9bqi0ThvbKs6Q91UOlyCEGY8hdBw6');") 70 if err != nil { 71 t.Fatalf("error setting up:%s", err) 72 } 73 74 query.ExecSQL("ALTER SEQUENCE users_id_seq RESTART WITH 1;") 75 76 // Insert story as a parent 77 _, err = query.ExecSQL("INSERT INTO stories (id,name,points) VALUES(1,'Story',100);") 78 if err != nil { 79 t.Fatalf("error setting up story:%s", err) 80 } 81 82 } 83 84 // Test GET /comments/create 85 func TestShowCreateComments(t *testing.T) { 86 87 // Setup request and recorder 88 r := httptest.NewRequest("GET", "/comments/create", nil) 89 w := httptest.NewRecorder() 90 91 // Set up comment session cookie for admin comment above 92 err := resource.AddUserSessionCookie(w, r, 1) 93 if err != nil { 94 t.Fatalf("commentactions: error setting session %s", err) 95 } 96 97 // Run the handler 98 err = HandleCreateShow(w, r) 99 100 // Test the error response 101 if err != nil || w.Code != http.StatusOK { 102 t.Fatalf("commentactions: error handling HandleCreateShow %s", err) 103 } 104 105 // Test the body for a known pattern 106 pattern := "resource-update-form" 107 if !strings.Contains(w.Body.String(), pattern) { 108 t.Fatalf("commentactions: unexpected response for HandleCreateShow expected:%s got:%s", pattern, w.Body.String()) 109 } 110 111 } 112 113 // Test POST /comments/create 114 func TestCreateComments(t *testing.T) { 115 116 form := url.Values{} 117 form.Add("user_name", names[0]) 118 form.Add("user_id", "1") 119 form.Add("story_id", "1") 120 form.Add("text", "foo bar comment") 121 body := strings.NewReader(form.Encode()) 122 123 r := httptest.NewRequest("POST", "/comments/create", body) 124 r.Header.Add("Content-Type", "application/x-www-form-urlencoded") 125 w := httptest.NewRecorder() 126 127 // Set up comment session cookie for admin comment 128 err := resource.AddUserSessionCookie(w, r, 1) 129 if err != nil { 130 t.Fatalf("commentactions: error setting session %s", err) 131 } 132 133 // Run the handler to update the comment 134 err = HandleCreate(w, r) 135 if err != nil { 136 t.Fatalf("commentactions: error handling HandleCreate %s", err) 137 } 138 139 // Test we get a redirect after update (to the comment concerned) 140 if w.Code != http.StatusFound { 141 t.Fatalf("commentactions: unexpected response code for HandleCreate expected:%d got:%d", http.StatusFound, w.Code) 142 } 143 144 // Check the comment name is in now value names[1] 145 allComments, err := comments.FindAll(comments.Query().Order("id desc")) 146 if err != nil || len(allComments) == 0 { 147 t.Fatalf("commentactions: error finding created comment %s", err) 148 } 149 newComments := allComments[0] 150 if newComments.ID != 1 || newComments.UserName != "admin" { // user name of admin user used to create 151 t.Fatalf("commentactions: error with created comment values: %v %s", newComments.ID, newComments.UserName) 152 } 153 } 154 155 // Test GET /comments 156 func TestListComments(t *testing.T) { 157 158 // Setup request and recorder 159 r := httptest.NewRequest("GET", "/comments", nil) 160 w := httptest.NewRecorder() 161 162 // Set up comment session cookie for admin comment above 163 err := resource.AddUserSessionCookie(w, r, 1) 164 if err != nil { 165 t.Fatalf("commentactions: error setting session %s", err) 166 } 167 168 // Run the handler 169 err = HandleIndex(w, r) 170 171 // Test the error response 172 if err != nil || w.Code != http.StatusOK { 173 t.Fatalf("commentactions: error handling HandleIndex %s", err) 174 } 175 176 // Test the body for a known pattern 177 pattern := `<ul class="comments">` 178 if !strings.Contains(w.Body.String(), pattern) { 179 t.Fatalf("commentactions: unexpected response for HandleIndex expected:%s got:%s", pattern, w.Body.String()) 180 } 181 182 } 183 184 // Test of GET /comments/1 185 func TestShowComments(t *testing.T) { 186 187 // Setup request and recorder 188 r := httptest.NewRequest("GET", "/comments/1", nil) 189 w := httptest.NewRecorder() 190 191 // Set up comment session cookie for admin comment above 192 err := resource.AddUserSessionCookie(w, r, 1) 193 if err != nil { 194 t.Fatalf("commentactions: error setting session %s", err) 195 } 196 197 // Run the handler 198 err = HandleShow(w, r) 199 200 // Test the error response 201 if err != nil || w.Code != http.StatusOK { 202 t.Fatalf("commentactions: error handling HandleShow %s", err) 203 } 204 205 // Test the body for a known pattern 206 pattern := names[0] 207 if !strings.Contains(w.Body.String(), names[0]) { 208 t.Fatalf("commentactions: unexpected response for HandleShow expected:%s got:%s", pattern, w.Body.String()) 209 } 210 } 211 212 // Test GET /comments/123/update 213 func TestShowUpdateComments(t *testing.T) { 214 215 // Setup request and recorder 216 r := httptest.NewRequest("GET", "/comments/1/update", nil) 217 w := httptest.NewRecorder() 218 219 // Set up comment session cookie for admin comment above 220 err := resource.AddUserSessionCookie(w, r, 1) 221 if err != nil { 222 t.Fatalf("commentactions: error setting session %s", err) 223 } 224 225 // Run the handler 226 err = HandleUpdateShow(w, r) 227 228 // Test the error response 229 if err != nil || w.Code != http.StatusOK { 230 t.Fatalf("commentactions: error handling HandleCreateShow %s", err) 231 } 232 233 // Test the body for a known pattern 234 pattern := "resource-update-form" 235 if !strings.Contains(w.Body.String(), pattern) { 236 t.Fatalf("commentactions: unexpected response for HandleCreateShow expected:%s got:%s", pattern, w.Body.String()) 237 } 238 239 } 240 241 // Test POST /comments/123/update 242 func TestUpdateComments(t *testing.T) { 243 244 form := url.Values{} 245 form.Add("user_name", names[1]) 246 body := strings.NewReader(form.Encode()) 247 248 r := httptest.NewRequest("POST", "/comments/1/update", body) 249 r.Header.Add("Content-Type", "application/x-www-form-urlencoded") 250 w := httptest.NewRecorder() 251 252 // Set up comment session cookie for admin comment 253 err := resource.AddUserSessionCookie(w, r, 1) 254 if err != nil { 255 t.Fatalf("commentactions: error setting session %s", err) 256 } 257 258 // Run the handler to update the comment 259 err = HandleUpdate(w, r) 260 if err != nil { 261 t.Fatalf("commentactions: error handling HandleUpdateComments %s", err) 262 } 263 264 // Test we get a redirect after update (to the comment concerned) 265 if w.Code != http.StatusFound { 266 t.Fatalf("commentactions: unexpected response code for HandleUpdateComments expected:%d got:%d", http.StatusFound, w.Code) 267 } 268 269 // Check the comment name is in now value names[1] 270 comment, err := comments.Find(1) 271 if err != nil { 272 t.Fatalf("commentactions: error finding updated comment %s", err) 273 } 274 if comment.ID != 1 || comment.UserName != names[1] { 275 t.Fatalf("commentactions: error with updated comment values: %v", comment) 276 } 277 278 } 279 280 // Test of POST /comments/123/destroy 281 func TestDeleteComments(t *testing.T) { 282 283 body := strings.NewReader(``) 284 285 // Now test deleting the comment created above as admin 286 r := httptest.NewRequest("POST", "/comments/1/destroy", body) 287 r.Header.Add("Content-Type", "application/x-www-form-urlencoded") 288 w := httptest.NewRecorder() 289 290 // Set up comment session cookie for admin comment 291 err := resource.AddUserSessionCookie(w, r, 1) 292 if err != nil { 293 t.Fatalf("commentactions: error setting session %s", err) 294 } 295 296 // Run the handler 297 err = HandleDestroy(w, r) 298 299 // Test the error response is 302 StatusFound 300 if err != nil { 301 t.Fatalf("commentactions: error handling HandleDestroy %s", err) 302 } 303 304 // Test we get a redirect after delete 305 if w.Code != http.StatusFound { 306 t.Fatalf("commentactions: unexpected response code for HandleDestroy expected:%d got:%d", http.StatusFound, w.Code) 307 } 308 // Now test as anon 309 r = httptest.NewRequest("POST", "/comments/1/destroy", body) 310 r.Header.Add("Content-Type", "application/x-www-form-urlencoded") 311 w = httptest.NewRecorder() 312 313 // Run the handler to test failure as anon 314 err = HandleDestroy(w, r) 315 if err == nil { // failure expected 316 t.Fatalf("commentactions: unexpected response for HandleDestroy as anon, expected failure") 317 } 318 319 }