eintopf.info@v0.13.16/test/integration/api_test.go (about) 1 // Copyright (C) 2022 The Eintopf authors 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <https://www.gnu.org/licenses/>. 15 16 package integration_test 17 18 import ( 19 "io" 20 "net/http" 21 "net/http/httptest" 22 "os" 23 "path" 24 "runtime" 25 "strings" 26 "testing" 27 28 "github.com/go-chi/chi/v5" 29 30 "eintopf.info/internal/mock" 31 "eintopf.info/service/action" 32 "eintopf.info/service/api" 33 "eintopf.info/service/event" 34 "eintopf.info/service/eventsearch" 35 "eintopf.info/service/group" 36 "eintopf.info/service/killswitch" 37 "eintopf.info/service/notification" 38 "eintopf.info/service/osm" 39 "eintopf.info/service/passwordrec" 40 "eintopf.info/service/user" 41 ) 42 43 func chDirProjectRoot() { 44 _, filename, _, _ := runtime.Caller(0) 45 dir := path.Join(path.Dir(filename), "../../") 46 err := os.Chdir(dir) 47 if err != nil { 48 panic(err) 49 } 50 } 51 52 func TestApi(t *testing.T) { 53 // change directory to project root, in order to find the api/swagger.json 54 // file. 55 chDirProjectRoot() 56 57 swaggerJSON, err := os.ReadFile("api/swagger.json") 58 if err != nil { 59 t.Fatal("failed to read api/swagger.json") 60 } 61 62 tests := []struct { 63 description string 64 url string 65 method string 66 body string 67 expectedBody string 68 expectedCode int 69 }{ 70 { 71 description: "serves swagger.json", 72 url: "/api/v1/swagger.json", 73 method: "GET", 74 body: "", 75 expectedBody: string(swaggerJSON), 76 expectedCode: 200, 77 }, 78 } 79 80 r := chi.NewRouter() 81 r.Group(api.Routes( 82 action.NewService(action.NewMemoryStore(), nil), 83 mock.NewAuthService(), 84 event.NewMemoryStore(), 85 &eventsearch.ServiceImpl{}, 86 group.NewMemoryStore(), 87 mock.NewInvitationService(), 88 killswitch.NewService(killswitch.NewMemoryStore(), nil), 89 mock.NewMediaService(), 90 notification.NewSettingsMemoryStore(), 91 notification.NewMemoryStore(), 92 &passwordrec.Service{}, 93 mock.NewPlaceService(), 94 mock.NewRepeatingEventService(), 95 mock.NewSearchService(), 96 mock.NewStatusService(), 97 user.NewService(user.NewMemoryStore()), 98 &osm.Service{}, 99 "", 100 )) 101 ts := httptest.NewServer(r) 102 defer ts.Close() 103 104 client := &http.Client{} 105 106 for _, tc := range tests { 107 t.Run(tc.description, func(tt *testing.T) { 108 req, err := http.NewRequest(tc.method, ts.URL+tc.url, strings.NewReader(tc.body)) 109 if err != nil { 110 tt.Fatalf("expected no error from http.NewRequest. got %s", err) 111 } 112 res, err := client.Do(req) 113 if err != nil { 114 tt.Fatalf("expected no error from client.Do. got %s", err) 115 } 116 if res != nil { 117 defer res.Body.Close() 118 } 119 120 b, err := io.ReadAll(res.Body) 121 if err != nil { 122 tt.Fatalf("expected no error from io.ReadAll. got %s", err) 123 } 124 125 if tc.expectedCode != res.StatusCode { 126 tt.Fatalf("expected status code %d. got %d", tc.expectedCode, res.StatusCode) 127 } 128 if tc.expectedBody != string(b) { 129 tt.Fatalf("expected body to be %s. got %s", tc.expectedBody, string(b)) 130 } 131 }) 132 } 133 }