github.com/jamiefdhurst/journal@v0.9.2/journal_test.go (about) 1 package main 2 3 import ( 4 "io" 5 "log" 6 "net/http" 7 "net/http/httptest" 8 "os" 9 "strings" 10 "testing" 11 12 "github.com/jamiefdhurst/journal/pkg/adapter/giphy" 13 "github.com/jamiefdhurst/journal/pkg/adapter/json" 14 15 "github.com/jamiefdhurst/journal/internal/app" 16 "github.com/jamiefdhurst/journal/internal/app/model" 17 "github.com/jamiefdhurst/journal/internal/app/router" 18 "github.com/jamiefdhurst/journal/pkg/database" 19 pkgrouter "github.com/jamiefdhurst/journal/pkg/router" 20 ) 21 22 var ( 23 rtr *pkgrouter.Router 24 server *httptest.Server 25 ) 26 27 func init() { 28 rtr = router.NewRouter(nil) 29 server = httptest.NewServer(rtr) 30 31 log.Println("Serving on " + server.URL) 32 } 33 34 func fixtures(t *testing.T) { 35 container := &app.Container{Configuration: app.DefaultConfiguration()} 36 adapter := giphy.Client{Client: &json.Client{}} 37 db := &database.Sqlite{} 38 if err := db.Connect("test/data/test.db"); err != nil { 39 t.Error("Could not open test database for writing...") 40 } 41 42 // Setup container 43 container.Db = db 44 container.Giphy = adapter 45 rtr.Container = container 46 47 js := model.Journals{Container: container} 48 db.Exec("DROP TABLE journal") 49 js.CreateTable() 50 51 // Set up data 52 db.Exec("INSERT INTO journal (slug, title, content, date) VALUES (?, ?, ?, ?)", "test", "Test", "<p>Test!</p>", "2018-01-01") 53 db.Exec("INSERT INTO journal (slug, title, content, date) VALUES (?, ?, ?, ?)", "test-2", "Another Test", "<p>Test again!</p>", "2018-02-01") 54 db.Exec("INSERT INTO journal (slug, title, content, date) VALUES (?, ?, ?, ?)", "test-3", "A Final Test", "<p>Test finally!</p>", "2018-03-01") 55 } 56 57 func TestConfig(t *testing.T) { 58 os.Setenv("J_TITLE", "A Test Title") 59 60 configuration := config() 61 62 if configuration.Title != "A Test Title" { 63 t.Error("Expected title to be set through environment") 64 } 65 if configuration.Port != "3000" { 66 t.Errorf("Expected default port to be set, got %s", configuration.Port) 67 } 68 } 69 70 func TestLoadDatabase(t *testing.T) { 71 container.Configuration.DatabasePath = "test/data/test.db" 72 closeFunc := loadDatabase() 73 closeFunc() 74 } 75 76 func TestLoadGiphy(t *testing.T) { 77 existing := os.Getenv("J_GIPHY_API_KEY") 78 os.Setenv("J_GIPHY_API_KEY", "foobar") 79 loadGiphy() 80 os.Setenv("J_GIPHY_API_KEY", existing) 81 82 if container.Giphy == nil { 83 t.Error("Expected Giphy adapter to be setup") 84 } 85 } 86 87 func TestApiv1List(t *testing.T) { 88 fixtures(t) 89 90 request, _ := http.NewRequest("GET", server.URL+"/api/v1/post", nil) 91 92 res, err := http.DefaultClient.Do(request) 93 94 if err != nil { 95 t.Errorf("Unexpected error: %s", err) 96 } 97 98 if res.StatusCode != 200 { 99 t.Error("Expected 200 status code") 100 } 101 102 defer res.Body.Close() 103 body, _ := io.ReadAll(res.Body) 104 expected := `[{"id":3,"slug":"test-3","title":"A Final Test","date":"2018-03-01T00:00:00Z","content":"<p>Test finally!</p>"},{"id":2,"slug":"test-2","title":"Another Test","date":"2018-02-01T00:00:00Z","content":"<p>Test again!</p>"},{"id":1,"slug":"test","title":"Test","date":"2018-01-01T00:00:00Z","content":"<p>Test!</p>"}]` 105 106 // Use contains to get rid of any extra whitespace that we can discount 107 if !strings.Contains(string(body[:]), expected) { 108 t.Errorf("Expected:\n\t%s\nGot:\n\t%s", expected, string(body[:])) 109 } 110 111 } 112 113 func TestApiV1Single(t *testing.T) { 114 fixtures(t) 115 116 request, _ := http.NewRequest("GET", server.URL+"/api/v1/post/test", nil) 117 118 res, err := http.DefaultClient.Do(request) 119 120 if err != nil { 121 t.Errorf("Unexpected error: %s", err) 122 } 123 124 if res.StatusCode != 200 { 125 t.Error("Expected 200 status code") 126 } 127 128 defer res.Body.Close() 129 body, _ := io.ReadAll(res.Body) 130 expected := `{"id":1,"slug":"test","title":"Test","date":"2018-01-01T00:00:00Z","content":"<p>Test!</p>"}` 131 132 // Use contains to get rid of any extra whitespace that we can discount 133 if !strings.Contains(string(body[:]), expected) { 134 t.Errorf("Expected:\n\t%s\nGot:\n\t%s", expected, string(body[:])) 135 } 136 } 137 138 func TestApiV1Single_NotFound(t *testing.T) { 139 fixtures(t) 140 141 request, _ := http.NewRequest("GET", server.URL+"/api/v1/post/random", nil) 142 143 res, err := http.DefaultClient.Do(request) 144 145 if err != nil { 146 t.Errorf("Unexpected error: %s", err) 147 } 148 149 if res.StatusCode != 404 { 150 t.Error("Expected 404 status code") 151 } 152 } 153 154 func TestApiV1Create(t *testing.T) { 155 fixtures(t) 156 157 request, _ := http.NewRequest("PUT", server.URL+"/api/v1/post", strings.NewReader(`{"title":"Test 4","date":"2018-06-01T00:00:00Z","content":"<p>Test 4!</p>"}`)) 158 159 res, err := http.DefaultClient.Do(request) 160 161 if err != nil { 162 t.Errorf("Unexpected error: %s", err) 163 } 164 165 if res.StatusCode != 201 { 166 t.Error("Expected 201 status code") 167 } 168 169 defer res.Body.Close() 170 body, _ := io.ReadAll(res.Body) 171 expected := `{"id":4,"slug":"test-4","title":"Test 4","date":"2018-06-01T00:00:00Z","content":"<p>Test 4!</p>"}` 172 173 // Use contains to get rid of any extra whitespace that we can discount 174 if !strings.Contains(string(body[:]), expected) { 175 t.Errorf("Expected:\n\t%s\nGot:\n\t%s", expected, string(body[:])) 176 } 177 } 178 179 func TestApiV1Create_InvalidRequest(t *testing.T) { 180 fixtures(t) 181 182 request, _ := http.NewRequest("PUT", server.URL+"/api/v1/post", nil) 183 184 res, err := http.DefaultClient.Do(request) 185 186 if err != nil { 187 t.Errorf("Unexpected error: %s", err) 188 } 189 190 if res.StatusCode != 400 { 191 t.Error("Expected 400 status code") 192 } 193 } 194 195 func TestApiV1Create_MissingData(t *testing.T) { 196 fixtures(t) 197 198 request, _ := http.NewRequest("PUT", server.URL+"/api/v1/post", strings.NewReader(`{"title":"Test 4"}`)) 199 200 res, err := http.DefaultClient.Do(request) 201 202 if err != nil { 203 t.Errorf("Unexpected error: %s", err) 204 } 205 206 if res.StatusCode != 400 { 207 t.Error("Expected 400 status code") 208 } 209 } 210 211 func TestApiV1Create_RepeatTitles(t *testing.T) { 212 fixtures(t) 213 214 request, _ := http.NewRequest("PUT", server.URL+"/api/v1/post", strings.NewReader(`{"title":"Repeated","date":"2018-02-01T00:00:00Z","content":"<p>Repeated content test!</p>"}`)) 215 res, err := http.DefaultClient.Do(request) 216 if err != nil { 217 t.Errorf("Unexpected error: %s", err) 218 } 219 if res.StatusCode != 201 { 220 t.Error("Expected 201 status code") 221 } 222 223 request, _ = http.NewRequest("PUT", server.URL+"/api/v1/post", strings.NewReader(`{"title":"Repeated","date":"2019-02-01T00:00:00Z","content":"<p>Repeated content test again!</p>"}`)) 224 res, err = http.DefaultClient.Do(request) 225 if err != nil { 226 t.Errorf("Unexpected error: %s", err) 227 } 228 if res.StatusCode != 201 { 229 t.Error("Expected 201 status code") 230 } 231 232 request, _ = http.NewRequest("GET", server.URL+"/api/v1/post/repeated-1", nil) 233 res, err = http.DefaultClient.Do(request) 234 if err != nil { 235 t.Errorf("Unexpected error: %s", err) 236 } 237 if res.StatusCode != 200 { 238 t.Error("Expected 200 status code") 239 } 240 defer res.Body.Close() 241 body, _ := io.ReadAll(res.Body) 242 expected := `{"id":5,"slug":"repeated-1","title":"Repeated","date":"2019-02-01T00:00:00Z","content":"<p>Repeated content test again!</p>"}` 243 244 // Use contains to get rid of any extra whitespace that we can discount 245 if !strings.Contains(string(body[:]), expected) { 246 t.Errorf("Expected:\n\t%s\nGot:\n\t%s", expected, string(body[:])) 247 } 248 } 249 250 func TestApiV1Update(t *testing.T) { 251 fixtures(t) 252 253 request, _ := http.NewRequest("POST", server.URL+"/api/v1/post/test", strings.NewReader(`{"title":"A different title"}`)) 254 255 res, err := http.DefaultClient.Do(request) 256 257 if err != nil { 258 t.Errorf("Unexpected error: %s", err) 259 } 260 261 if res.StatusCode != 200 { 262 t.Error("Expected 200 status code") 263 } 264 265 defer res.Body.Close() 266 body, _ := io.ReadAll(res.Body) 267 expected := `{"id":1,"slug":"test","title":"A different title","date":"2018-01-01T00:00:00Z","content":"<p>Test!</p>"}` 268 269 // Use contains to get rid of any extra whitespace that we can discount 270 if !strings.Contains(string(body[:]), expected) { 271 t.Errorf("Expected:\n\t%s\nGot:\n\t%s", expected, string(body[:])) 272 } 273 } 274 275 func TestApiV1Update_NotFound(t *testing.T) { 276 fixtures(t) 277 278 request, _ := http.NewRequest("POST", server.URL+"/api/v1/post/random", strings.NewReader(`{"title":"A different title"}`)) 279 280 res, err := http.DefaultClient.Do(request) 281 282 if err != nil { 283 t.Errorf("Unexpected error: %s", err) 284 } 285 286 if res.StatusCode != 404 { 287 t.Error("Expected 404 status code") 288 } 289 } 290 291 func TestApiV1Update_InvalidRequest(t *testing.T) { 292 fixtures(t) 293 294 request, _ := http.NewRequest("POST", server.URL+"/api/v1/post/test", nil) 295 296 res, err := http.DefaultClient.Do(request) 297 298 if err != nil { 299 t.Errorf("Unexpected error: %s", err) 300 } 301 302 if res.StatusCode != 400 { 303 t.Error("Expected 400 status code") 304 } 305 }