github.com/saadullahsaeed/fragmenta-cms@v1.5.4/src/pages/actions/actions_test.go (about) 1 package pageactions 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/fragmenta/fragmenta-cms/src/lib/resource" 15 "github.com/fragmenta/fragmenta-cms/src/pages" 16 ) 17 18 // names is used to test setting and getting the first string field of the page. 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("pages: 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("/pages", nil) 42 router.Add("/pages/create", nil) 43 router.Add("/pages/create", nil).Post() 44 router.Add("/pages/login", nil) 45 router.Add("/pages/login", nil).Post() 46 router.Add("/pages/login", nil).Post() 47 router.Add("/pages/logout", nil).Post() 48 router.Add("/pages/{id:\\d+}/update", nil) 49 router.Add("/pages/{id:\\d+}/update", nil).Post() 50 router.Add("/pages/{id:\\d+}/destroy", nil).Post() 51 router.Add("/pages/{id:\\d+}", nil) 52 53 // Delete all pages to ensure we get consistent results 54 query.ExecSQL("delete from pages;") 55 query.ExecSQL("ALTER SEQUENCE pages_id_seq RESTART WITH 1;") 56 } 57 58 // Test GET /pages/create 59 func TestShowCreatePage(t *testing.T) { 60 61 // Setup request and recorder 62 r := httptest.NewRequest("GET", "/pages/create", nil) 63 w := httptest.NewRecorder() 64 65 // Set up page session cookie for admin page above 66 err := resource.AddUserSessionCookie(w, r, 1) 67 if err != nil { 68 t.Fatalf("pageactions: error setting session %s", err) 69 } 70 71 // Run the handler 72 err = HandleCreateShow(w, r) 73 74 // Test the error response 75 if err != nil || w.Code != http.StatusOK { 76 t.Fatalf("pageactions: error handling HandleCreateShow %s", err) 77 } 78 79 // Test the body for a known pattern 80 pattern := "resource-update-form" 81 if !strings.Contains(w.Body.String(), pattern) { 82 t.Fatalf("pageactions: unexpected response for HandleCreateShow expected:%s got:%s", pattern, w.Body.String()) 83 } 84 85 } 86 87 // Test POST /pages/create 88 func TestCreatePage(t *testing.T) { 89 90 form := url.Values{} 91 form.Add("name", names[0]) 92 body := strings.NewReader(form.Encode()) 93 94 r := httptest.NewRequest("POST", "/pages/create", body) 95 r.Header.Add("Content-Type", "application/x-www-form-urlencoded") 96 w := httptest.NewRecorder() 97 98 // Set up page session cookie for admin page 99 err := resource.AddUserSessionCookie(w, r, 1) 100 if err != nil { 101 t.Fatalf("pageactions: error setting session %s", err) 102 } 103 104 // Run the handler to update the page 105 err = HandleCreate(w, r) 106 if err != nil { 107 t.Fatalf("pageactions: error handling HandleCreate %s", err) 108 } 109 110 // Test we get a redirect after update (to the page concerned) 111 if w.Code != http.StatusFound { 112 t.Fatalf("pageactions: unexpected response code for HandleCreate expected:%d got:%d", http.StatusFound, w.Code) 113 } 114 115 // Check the page name is in now value names[1] 116 allPage, err := pages.FindAll(pages.Query().Order("id desc")) 117 if err != nil || len(allPage) == 0 { 118 t.Fatalf("pageactions: error finding created page %s", err) 119 } 120 newPage := allPage[0] 121 if newPage.ID != 1 || newPage.Name != names[0] { 122 t.Fatalf("pageactions: error with created page values: %v %s", newPage.ID, newPage.Name) 123 } 124 } 125 126 // Test GET /pages 127 func TestListPage(t *testing.T) { 128 129 // Setup request and recorder 130 r := httptest.NewRequest("GET", "/pages", nil) 131 w := httptest.NewRecorder() 132 133 // Set up page session cookie for admin page above 134 err := resource.AddUserSessionCookie(w, r, 1) 135 if err != nil { 136 t.Fatalf("pageactions: error setting session %s", err) 137 } 138 139 // Run the handler 140 err = HandleIndex(w, r) 141 142 // Test the error response 143 if err != nil || w.Code != http.StatusOK { 144 t.Fatalf("pageactions: error handling HandleIndex %s", err) 145 } 146 147 // Test the body for a known pattern 148 pattern := "data-table-head" 149 if !strings.Contains(w.Body.String(), pattern) { 150 t.Fatalf("pageactions: unexpected response for HandleIndex expected:%s got:%s", pattern, w.Body.String()) 151 } 152 153 } 154 155 // Test of GET /pages/1 156 func TestShowPage(t *testing.T) { 157 158 // Setup request and recorder 159 r := httptest.NewRequest("GET", "/pages/1", nil) 160 w := httptest.NewRecorder() 161 162 // Set up page session cookie for admin page above 163 err := resource.AddUserSessionCookie(w, r, 1) 164 if err != nil { 165 t.Fatalf("pageactions: error setting session %s", err) 166 } 167 168 // Run the handler 169 err = HandleShow(w, r) 170 171 // Test the error response 172 if err != nil || w.Code != http.StatusOK { 173 t.Fatalf("pageactions: error handling HandleShow %s", err) 174 } 175 176 // Test the body for a known pattern 177 pattern := names[0] 178 if !strings.Contains(w.Body.String(), names[0]) { 179 t.Fatalf("pageactions: unexpected response for HandleShow expected:%s got:%s", pattern, w.Body.String()) 180 } 181 } 182 183 // Test GET /pages/123/update 184 func TestShowUpdatePage(t *testing.T) { 185 186 // Setup request and recorder 187 r := httptest.NewRequest("GET", "/pages/1/update", nil) 188 w := httptest.NewRecorder() 189 190 // Set up page session cookie for admin page above 191 err := resource.AddUserSessionCookie(w, r, 1) 192 if err != nil { 193 t.Fatalf("pageactions: error setting session %s", err) 194 } 195 196 // Run the handler 197 err = HandleUpdateShow(w, r) 198 199 // Test the error response 200 if err != nil || w.Code != http.StatusOK { 201 t.Fatalf("pageactions: error handling HandleCreateShow %s", err) 202 } 203 204 // Test the body for a known pattern 205 pattern := "resource-update-form" 206 if !strings.Contains(w.Body.String(), pattern) { 207 t.Fatalf("pageactions: unexpected response for HandleCreateShow expected:%s got:%s", pattern, w.Body.String()) 208 } 209 210 } 211 212 // Test POST /pages/123/update 213 func TestUpdatePage(t *testing.T) { 214 215 form := url.Values{} 216 form.Add("name", names[1]) 217 body := strings.NewReader(form.Encode()) 218 219 r := httptest.NewRequest("POST", "/pages/1/update", body) 220 r.Header.Add("Content-Type", "application/x-www-form-urlencoded") 221 w := httptest.NewRecorder() 222 223 // Set up page session cookie for admin page 224 err := resource.AddUserSessionCookie(w, r, 1) 225 if err != nil { 226 t.Fatalf("pageactions: error setting session %s", err) 227 } 228 229 // Run the handler to update the page 230 err = HandleUpdate(w, r) 231 if err != nil { 232 t.Fatalf("pageactions: error handling HandleUpdatePage %s", err) 233 } 234 235 // Test we get a redirect after update (to the page concerned) 236 if w.Code != http.StatusFound { 237 t.Fatalf("pageactions: unexpected response code for HandleUpdatePage expected:%d got:%d", http.StatusFound, w.Code) 238 } 239 240 // Check the page name is in now value names[1] 241 page, err := pages.Find(1) 242 if err != nil { 243 t.Fatalf("pageactions: error finding updated page %s", err) 244 } 245 if page.ID != 1 || page.Name != names[1] { 246 t.Fatalf("pageactions: error with updated page values: %v", page) 247 } 248 249 } 250 251 // Test of POST /pages/123/destroy 252 func TestDeletePage(t *testing.T) { 253 254 body := strings.NewReader(``) 255 256 // Now test deleting the page created above as admin 257 r := httptest.NewRequest("POST", "/pages/1/destroy", body) 258 r.Header.Add("Content-Type", "application/x-www-form-urlencoded") 259 w := httptest.NewRecorder() 260 261 // Set up page session cookie for admin page 262 err := resource.AddUserSessionCookie(w, r, 1) 263 if err != nil { 264 t.Fatalf("pageactions: error setting session %s", err) 265 } 266 267 // Run the handler 268 err = HandleDestroy(w, r) 269 270 // Test the error response is 302 StatusFound 271 if err != nil { 272 t.Fatalf("pageactions: error handling HandleDestroy %s", err) 273 } 274 275 // Test we get a redirect after delete 276 if w.Code != http.StatusFound { 277 t.Fatalf("pageactions: unexpected response code for HandleDestroy expected:%d got:%d", http.StatusFound, w.Code) 278 } 279 // Now test as anon 280 r = httptest.NewRequest("POST", "/pages/1/destroy", body) 281 r.Header.Add("Content-Type", "application/x-www-form-urlencoded") 282 w = httptest.NewRecorder() 283 284 // Run the handler to test failure as anon 285 err = HandleDestroy(w, r) 286 if err == nil { // failure expected 287 t.Fatalf("pageactions: unexpected response for HandleDestroy as anon, expected failure") 288 } 289 290 }