Reorganize api tests
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
parent
0e76cad5d7
commit
889894a737
169
api/http_test.go
169
api/http_test.go
@ -21,16 +21,17 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestHandlers(t *testing.T) {
|
func TestHandlers(t *testing.T) {
|
||||||
cfg := &gpaste.ServerConfig{
|
//cfg := &gpaste.ServerConfig{
|
||||||
SigningSecret: "abc123",
|
// SigningSecret: "abc123",
|
||||||
Store: &gpaste.ServerStoreConfig{
|
// Store: &gpaste.ServerStoreConfig{
|
||||||
Type: "memory",
|
// Type: "memory",
|
||||||
},
|
// },
|
||||||
URL: "http://localhost:8080",
|
// URL: "http://localhost:8080",
|
||||||
}
|
//}
|
||||||
hs := api.NewHTTPServer(cfg)
|
//hs := api.NewHTTPServer(cfg)
|
||||||
|
|
||||||
t.Run("HandlerIndex", func(t *testing.T) {
|
t.Run("index", func(t *testing.T) {
|
||||||
|
hs := newServer()
|
||||||
rr := httptest.NewRecorder()
|
rr := httptest.NewRecorder()
|
||||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||||
|
|
||||||
@ -45,7 +46,11 @@ func TestHandlers(t *testing.T) {
|
|||||||
t.Errorf("Body does not match expected. Got %s want %s", body, expectedBody)
|
t.Errorf("Body does not match expected. Got %s want %s", body, expectedBody)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
t.Run("HandlerAPIFilePost", func(t *testing.T) {
|
t.Run("api", func(t *testing.T) {
|
||||||
|
t.Run("file", func(t *testing.T) {
|
||||||
|
// POST /api/file
|
||||||
|
t.Run("POST", func(t *testing.T) {
|
||||||
|
hs := newServer()
|
||||||
rr := httptest.NewRecorder()
|
rr := httptest.NewRecorder()
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
mw := multipart.NewWriter(buf)
|
mw := multipart.NewWriter(buf)
|
||||||
@ -88,9 +93,32 @@ func TestHandlers(t *testing.T) {
|
|||||||
t.Errorf("Response has empty id")
|
t.Errorf("Response has empty id")
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Run("HandlerAPIFileGet", func(t *testing.T) {
|
retrieved, err := hs.Files.Get(uploadID)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Error retrieving file: %s", err)
|
||||||
|
}
|
||||||
|
defer retrieved.Body.Close()
|
||||||
|
retBuf := new(bytes.Buffer)
|
||||||
|
io.Copy(retBuf, retrieved.Body)
|
||||||
|
if diff := cmp.Diff(retBuf.String(), expectedData); diff != "" {
|
||||||
|
t.Errorf("Retrieved file mismatch: %s", diff)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
// GET /api/file/id
|
||||||
|
t.Run("GET", func(t *testing.T) {
|
||||||
|
hs := newServer()
|
||||||
|
fileData := "abc123456"
|
||||||
|
sr := io.NopCloser(strings.NewReader(fileData))
|
||||||
|
file := &files.File{
|
||||||
|
ID: uuid.NewString(),
|
||||||
|
OriginalFilename: "test-file.txt",
|
||||||
|
MaxViews: 99,
|
||||||
|
ExpiresOn: time.Now().Add(90 * time.Second),
|
||||||
|
Body: sr,
|
||||||
|
}
|
||||||
|
hs.Files.Store(file)
|
||||||
rr := httptest.NewRecorder()
|
rr := httptest.NewRecorder()
|
||||||
url := fmt.Sprintf("/api/file/%s", uploadID)
|
url := fmt.Sprintf("/api/file/%s", file.ID)
|
||||||
req := httptest.NewRequest(http.MethodGet, url, nil)
|
req := httptest.NewRequest(http.MethodGet, url, nil)
|
||||||
|
|
||||||
hs.Handler.ServeHTTP(rr, req)
|
hs.Handler.ServeHTTP(rr, req)
|
||||||
@ -99,20 +127,14 @@ func TestHandlers(t *testing.T) {
|
|||||||
t.Errorf("Returned unexpected status. Got %d want %d", status, http.StatusAccepted)
|
t.Errorf("Returned unexpected status. Got %d want %d", status, http.StatusAccepted)
|
||||||
t.Logf(url)
|
t.Logf(url)
|
||||||
}
|
}
|
||||||
if body := rr.Body.String(); body != expectedData {
|
if diff := cmp.Diff(rr.Body.String(), fileData); diff != "" {
|
||||||
t.Errorf("Returned body does not match expected.")
|
t.Errorf("Returned body does not match expected: %s", diff)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
|
||||||
t.Run("HandlerAPIFileDelete", func(t *testing.T) {
|
// DELETE /api/file/id
|
||||||
cfg := &gpaste.ServerConfig{
|
t.Run("DELETE", func(t *testing.T) {
|
||||||
SigningSecret: "abc123",
|
hs := newServer()
|
||||||
Store: &gpaste.ServerStoreConfig{
|
|
||||||
Type: "memory",
|
|
||||||
},
|
|
||||||
URL: "http://localhost:8080",
|
|
||||||
}
|
|
||||||
hs := api.NewHTTPServer(cfg)
|
|
||||||
fileBody := io.NopCloser(strings.NewReader("roflcopter"))
|
fileBody := io.NopCloser(strings.NewReader("roflcopter"))
|
||||||
file := &files.File{
|
file := &files.File{
|
||||||
ID: uuid.NewString(),
|
ID: uuid.NewString(),
|
||||||
@ -138,54 +160,12 @@ func TestHandlers(t *testing.T) {
|
|||||||
if _, err := hs.Files.Get(file.ID); err == nil {
|
if _, err := hs.Files.Get(file.ID); err == nil {
|
||||||
t.Errorf("Getting after delete returned no error")
|
t.Errorf("Getting after delete returned no error")
|
||||||
}
|
}
|
||||||
|
|
||||||
})
|
})
|
||||||
t.Run("HandlerAPILogin", func(t *testing.T) {
|
|
||||||
// TODO: Add test
|
|
||||||
username := "admin"
|
|
||||||
password := "admin"
|
|
||||||
user := &users.User{Username: username}
|
|
||||||
if err := user.SetPassword(password); err != nil {
|
|
||||||
t.Fatalf("Error setting user password: %s", err)
|
|
||||||
}
|
|
||||||
if err := hs.Users.Store(user); err != nil {
|
|
||||||
t.Fatalf("Error storing user: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
requestData := struct {
|
|
||||||
Username string `json:"username"`
|
|
||||||
Password string `json:"password"`
|
|
||||||
}{
|
|
||||||
Username: username,
|
|
||||||
Password: password,
|
|
||||||
}
|
|
||||||
|
|
||||||
body := new(bytes.Buffer)
|
|
||||||
encoder := json.NewEncoder(body)
|
|
||||||
if err := encoder.Encode(&requestData); err != nil {
|
|
||||||
t.Fatalf("Error encoding request body: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
rr := httptest.NewRecorder()
|
|
||||||
req := httptest.NewRequest(http.MethodPost, "/api/login", body)
|
|
||||||
|
|
||||||
hs.Handler.ServeHTTP(rr, req)
|
|
||||||
|
|
||||||
responseData := struct {
|
|
||||||
Token string `json:"token"`
|
|
||||||
}{}
|
|
||||||
|
|
||||||
decoder := json.NewDecoder(rr.Body)
|
|
||||||
if err := decoder.Decode(&responseData); err != nil {
|
|
||||||
t.Fatalf("Error decoding response: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := hs.Auth.ValidateToken(responseData.Token); err != nil {
|
|
||||||
t.Fatalf("Unable to validate received token: %s", err)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
t.Run("User", func(t *testing.T) {
|
// /api/user
|
||||||
t.Run("Create", func(t *testing.T) {
|
|
||||||
|
t.Run("user", func(t *testing.T) {
|
||||||
|
t.Run("POST", func(t *testing.T) {
|
||||||
hs := newServer()
|
hs := newServer()
|
||||||
adminPw := "admin"
|
adminPw := "admin"
|
||||||
admin := &users.User{
|
admin := &users.User{
|
||||||
@ -237,6 +217,55 @@ func TestHandlers(t *testing.T) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// /api/login
|
||||||
|
t.Run("Login", func(t *testing.T) {
|
||||||
|
hs := newServer()
|
||||||
|
// TODO: Add test
|
||||||
|
username := "admin"
|
||||||
|
password := "admin"
|
||||||
|
user := &users.User{Username: username}
|
||||||
|
if err := user.SetPassword(password); err != nil {
|
||||||
|
t.Fatalf("Error setting user password: %s", err)
|
||||||
|
}
|
||||||
|
if err := hs.Users.Store(user); err != nil {
|
||||||
|
t.Fatalf("Error storing user: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
requestData := struct {
|
||||||
|
Username string `json:"username"`
|
||||||
|
Password string `json:"password"`
|
||||||
|
}{
|
||||||
|
Username: username,
|
||||||
|
Password: password,
|
||||||
|
}
|
||||||
|
|
||||||
|
body := new(bytes.Buffer)
|
||||||
|
encoder := json.NewEncoder(body)
|
||||||
|
if err := encoder.Encode(&requestData); err != nil {
|
||||||
|
t.Fatalf("Error encoding request body: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
rr := httptest.NewRecorder()
|
||||||
|
req := httptest.NewRequest(http.MethodPost, "/api/login", body)
|
||||||
|
|
||||||
|
hs.Handler.ServeHTTP(rr, req)
|
||||||
|
|
||||||
|
responseData := struct {
|
||||||
|
Token string `json:"token"`
|
||||||
|
}{}
|
||||||
|
|
||||||
|
decoder := json.NewDecoder(rr.Body)
|
||||||
|
if err := decoder.Decode(&responseData); err != nil {
|
||||||
|
t.Fatalf("Error decoding response: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := hs.Auth.ValidateToken(responseData.Token); err != nil {
|
||||||
|
t.Fatalf("Unable to validate received token: %s", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func newServer() *api.HTTPServer {
|
func newServer() *api.HTTPServer {
|
||||||
|
Loading…
Reference in New Issue
Block a user