Add delete
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Torjus Håkestad 2022-01-21 07:17:52 +01:00
parent ff8c6aca64
commit ed4a10c966
5 changed files with 101 additions and 0 deletions

View File

@ -49,6 +49,7 @@ func NewHTTPServer(cfg *gpaste.ServerConfig) *HTTPServer {
r.Get("/", srv.HandlerIndex) r.Get("/", srv.HandlerIndex)
r.Post("/api/file", srv.HandlerAPIFilePost) r.Post("/api/file", srv.HandlerAPIFilePost)
r.Get("/api/file/{id}", srv.HandlerAPIFileGet) r.Get("/api/file/{id}", srv.HandlerAPIFileGet)
r.Delete("/api/file/{id}", srv.HandlerAPIFileDelete)
r.Post("/api/login", srv.HandlerAPILogin) r.Post("/api/login", srv.HandlerAPILogin)
r.Post("/api/user", srv.HandlerAPIUserCreate) r.Post("/api/user", srv.HandlerAPIUserCreate)
srv.Handler = r srv.Handler = r
@ -117,6 +118,23 @@ func (s *HTTPServer) HandlerAPIFileGet(w http.ResponseWriter, r *http.Request) {
} }
} }
func (s *HTTPServer) HandlerAPIFileDelete(w http.ResponseWriter, r *http.Request) {
// TODO: Require auth
id := chi.URLParam(r, "id")
if id == "" {
w.WriteHeader(http.StatusBadRequest)
return
}
err := s.Files.Delete(id)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
reqID := middleware.GetReqID(r.Context())
s.Logger.Infow("Deleted file", "id", id, "req_id", reqID)
}
func (s *HTTPServer) processMultiPartFormUpload(w http.ResponseWriter, r *http.Request) { func (s *HTTPServer) processMultiPartFormUpload(w http.ResponseWriter, r *http.Request) {
reqID := middleware.GetReqID(r.Context()) reqID := middleware.GetReqID(r.Context())

View File

@ -8,11 +8,15 @@ import (
"mime/multipart" "mime/multipart"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"strings"
"testing" "testing"
"time"
"git.t-juice.club/torjus/gpaste" "git.t-juice.club/torjus/gpaste"
"git.t-juice.club/torjus/gpaste/api" "git.t-juice.club/torjus/gpaste/api"
"git.t-juice.club/torjus/gpaste/files"
"git.t-juice.club/torjus/gpaste/users" "git.t-juice.club/torjus/gpaste/users"
"github.com/google/uuid"
) )
func TestHandlers(t *testing.T) { func TestHandlers(t *testing.T) {
@ -99,6 +103,42 @@ func TestHandlers(t *testing.T) {
} }
}) })
}) })
t.Run("HandlerAPIFileDelete", func(t *testing.T) {
cfg := &gpaste.ServerConfig{
SigningSecret: "abc123",
Store: &gpaste.ServerStoreConfig{
Type: "memory",
},
URL: "http://localhost:8080",
}
hs := api.NewHTTPServer(cfg)
fileBody := io.NopCloser(strings.NewReader("roflcopter"))
file := &files.File{
ID: uuid.NewString(),
OriginalFilename: "testpls.txt",
MaxViews: 9,
ExpiresOn: time.Now().Add(10 * time.Hour),
Body: fileBody,
}
if err := hs.Files.Store(file); err != nil {
t.Fatalf("Error storing file: %s", err)
}
rr := httptest.NewRecorder()
url := fmt.Sprintf("/api/file/%s", file.ID)
req := httptest.NewRequest(http.MethodDelete, url, nil)
hs.Handler.ServeHTTP(rr, req)
if rr.Result().StatusCode != http.StatusOK {
t.Fatalf("Delete returned wrong status: %s", rr.Result().Status)
}
if _, err := hs.Files.Get(file.ID); err == nil {
t.Errorf("Getting after delete returned no error")
}
})
t.Run("HandlerAPILogin", func(t *testing.T) { t.Run("HandlerAPILogin", func(t *testing.T) {
// TODO: Add test // TODO: Add test
username := "admin" username := "admin"

View File

@ -203,3 +203,24 @@ func (c *Client) Upload(ctx context.Context, files ...*files.File) ([]api.Respon
return expectedResp, nil return expectedResp, nil
} }
func (c *Client) Delete(ctx context.Context, id string) error {
url := fmt.Sprintf("%s/api/file/%s", c.BaseURL, id)
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, url, nil)
if err != nil {
return fmt.Errorf("error creating request: %w", err)
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.AuthToken))
resp, err := c.httpClient.Do(req)
if err != nil {
return fmt.Errorf("unable to perform request: %s", err)
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("got non-ok response from server: %s", resp.Status)
}
return nil
}

View File

@ -39,6 +39,22 @@ func ActionUpload(c *cli.Context) error {
return nil return nil
} }
func ActionDelete(c *cli.Context) error {
clnt := client.Client{
BaseURL: c.String("url"),
}
for _, arg := range c.Args().Slice() {
ctx, cancel := context.WithTimeout(c.Context, 5*time.Second)
defer cancel()
if err := clnt.Delete(ctx, arg); err != nil {
fmt.Printf("Error deleting file %s\n", arg)
fmt.Printf("%s\n", err)
}
fmt.Printf("Deleted %s\n", arg)
}
return nil
}
func ActionLogin(c *cli.Context) error { func ActionLogin(c *cli.Context) error {
username := c.Args().First() username := c.Args().First()
if username == "" { if username == "" {

View File

@ -37,6 +37,12 @@ func main() {
ArgsUsage: "FILE [FILE]...", ArgsUsage: "FILE [FILE]...",
Action: actions.ActionUpload, Action: actions.ActionUpload,
}, },
{
Name: "delete",
Usage: "Delete file(s)",
ArgsUsage: "FILE [FILE]...",
Action: actions.ActionDelete,
},
{ {
Name: "login", Name: "login",
Usage: "Login to gpaste server", Usage: "Login to gpaste server",