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
357
api/http_test.go
357
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,198 +46,226 @@ 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) {
|
||||||
rr := httptest.NewRecorder()
|
t.Run("file", func(t *testing.T) {
|
||||||
buf := &bytes.Buffer{}
|
// POST /api/file
|
||||||
mw := multipart.NewWriter(buf)
|
t.Run("POST", func(t *testing.T) {
|
||||||
fw, err := mw.CreateFormFile("test", "test.txt")
|
hs := newServer()
|
||||||
if err != nil {
|
rr := httptest.NewRecorder()
|
||||||
t.Fatalf("Unable to create form file: %s", err)
|
buf := &bytes.Buffer{}
|
||||||
}
|
mw := multipart.NewWriter(buf)
|
||||||
expectedData := "Test OMEGALUL PLS."
|
fw, err := mw.CreateFormFile("test", "test.txt")
|
||||||
if _, err := io.WriteString(fw, expectedData); err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Unable to write body to buffer: %s", err)
|
t.Fatalf("Unable to create form file: %s", err)
|
||||||
}
|
}
|
||||||
mw.Close()
|
expectedData := "Test OMEGALUL PLS."
|
||||||
|
if _, err := io.WriteString(fw, expectedData); err != nil {
|
||||||
|
t.Fatalf("Unable to write body to buffer: %s", err)
|
||||||
|
}
|
||||||
|
mw.Close()
|
||||||
|
|
||||||
req := httptest.NewRequest(http.MethodPost, "/api/file", buf)
|
req := httptest.NewRequest(http.MethodPost, "/api/file", buf)
|
||||||
req.Header.Add("Content-Type", mw.FormDataContentType())
|
req.Header.Add("Content-Type", mw.FormDataContentType())
|
||||||
|
|
||||||
hs.Handler.ServeHTTP(rr, req)
|
hs.Handler.ServeHTTP(rr, req)
|
||||||
|
|
||||||
if status := rr.Code; status != http.StatusAccepted {
|
if status := rr.Code; status != http.StatusAccepted {
|
||||||
t.Errorf("Returned unexpected status. Got %d want %d", status, http.StatusAccepted)
|
t.Errorf("Returned unexpected status. Got %d want %d", status, http.StatusAccepted)
|
||||||
}
|
}
|
||||||
|
|
||||||
var expectedResp []struct {
|
var expectedResp []struct {
|
||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
URL string `json:"url"`
|
URL string `json:"url"`
|
||||||
}
|
}
|
||||||
|
|
||||||
decoder := json.NewDecoder(rr.Result().Body)
|
decoder := json.NewDecoder(rr.Result().Body)
|
||||||
if err := decoder.Decode(&expectedResp); err != nil {
|
if err := decoder.Decode(&expectedResp); err != nil {
|
||||||
t.Fatalf("error decoding response: %s", err)
|
t.Fatalf("error decoding response: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if l := len(expectedResp); l != 1 {
|
if l := len(expectedResp); l != 1 {
|
||||||
t.Errorf("Response has wrong length. Got %d want %d", l, 1)
|
t.Errorf("Response has wrong length. Got %d want %d", l, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
uploadID := expectedResp[0].ID
|
uploadID := expectedResp[0].ID
|
||||||
if uploadID == "" {
|
if uploadID == "" {
|
||||||
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)
|
||||||
rr := httptest.NewRecorder()
|
if err != nil {
|
||||||
url := fmt.Sprintf("/api/file/%s", uploadID)
|
t.Errorf("Error retrieving file: %s", err)
|
||||||
req := httptest.NewRequest(http.MethodGet, url, nil)
|
}
|
||||||
|
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()
|
||||||
|
url := fmt.Sprintf("/api/file/%s", file.ID)
|
||||||
|
req := httptest.NewRequest(http.MethodGet, url, nil)
|
||||||
|
|
||||||
hs.Handler.ServeHTTP(rr, req)
|
hs.Handler.ServeHTTP(rr, req)
|
||||||
|
|
||||||
if status := rr.Code; status != http.StatusOK {
|
if status := rr.Code; status != http.StatusOK {
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// DELETE /api/file/id
|
||||||
|
t.Run("DELETE", func(t *testing.T) {
|
||||||
|
hs := newServer()
|
||||||
|
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")
|
||||||
|
}
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
// /api/user
|
||||||
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.Run("user", func(t *testing.T) {
|
||||||
t.Fatalf("Error storing file: %s", err)
|
t.Run("POST", func(t *testing.T) {
|
||||||
}
|
hs := newServer()
|
||||||
|
adminPw := "admin"
|
||||||
|
admin := &users.User{
|
||||||
|
Username: "admin",
|
||||||
|
Role: users.RoleAdmin,
|
||||||
|
}
|
||||||
|
_ = admin.SetPassword(adminPw)
|
||||||
|
_ = hs.Users.Store(admin)
|
||||||
|
|
||||||
rr := httptest.NewRecorder()
|
token, err := hs.Auth.Login(admin.Username, adminPw)
|
||||||
url := fmt.Sprintf("/api/file/%s", file.ID)
|
if err != nil {
|
||||||
req := httptest.NewRequest(http.MethodDelete, url, nil)
|
t.Fatalf("error getting admin token: %s", err)
|
||||||
hs.Handler.ServeHTTP(rr, req)
|
}
|
||||||
|
|
||||||
if rr.Result().StatusCode != http.StatusOK {
|
requestData := &api.RequestAPIUserCreate{
|
||||||
t.Fatalf("Delete returned wrong status: %s", rr.Result().Status)
|
Username: "test",
|
||||||
}
|
Password: "test",
|
||||||
|
}
|
||||||
|
body := new(bytes.Buffer)
|
||||||
|
encoder := json.NewEncoder(body)
|
||||||
|
if err := encoder.Encode(requestData); err != nil {
|
||||||
|
t.Fatalf("Error encoding data: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
if _, err := hs.Files.Get(file.ID); err == nil {
|
rr := httptest.NewRecorder()
|
||||||
t.Errorf("Getting after delete returned no error")
|
req := httptest.NewRequest(http.MethodPost, "/api/user", body)
|
||||||
}
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
|
||||||
|
hs.Handler.ServeHTTP(rr, req)
|
||||||
|
|
||||||
})
|
if rr.Result().StatusCode != http.StatusAccepted {
|
||||||
t.Run("HandlerAPILogin", func(t *testing.T) {
|
t.Fatalf("Create returned wrong status: %s", rr.Result().Status)
|
||||||
// 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 {
|
user, err := hs.Users.Get(requestData.Username)
|
||||||
Username string `json:"username"`
|
if err != nil {
|
||||||
Password string `json:"password"`
|
t.Fatalf("Unable to get user after create: %s", err)
|
||||||
}{
|
}
|
||||||
Username: username,
|
|
||||||
Password: password,
|
|
||||||
}
|
|
||||||
|
|
||||||
body := new(bytes.Buffer)
|
expectedUser := &users.User{
|
||||||
encoder := json.NewEncoder(body)
|
Username: requestData.Username,
|
||||||
if err := encoder.Encode(&requestData); err != nil {
|
Role: users.RoleUser,
|
||||||
t.Fatalf("Error encoding request body: %s", err)
|
}
|
||||||
}
|
ignorePW := cmp.FilterPath(func(p cmp.Path) bool {
|
||||||
|
return p.String() == "HashedPassword"
|
||||||
|
}, cmp.Ignore())
|
||||||
|
|
||||||
rr := httptest.NewRecorder()
|
if diff := cmp.Diff(user, expectedUser, ignorePW); diff != "" {
|
||||||
req := httptest.NewRequest(http.MethodPost, "/api/login", body)
|
t.Errorf("User does not match expected: %s", diff)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
hs.Handler.ServeHTTP(rr, req)
|
// /api/login
|
||||||
|
t.Run("Login", func(t *testing.T) {
|
||||||
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) {
|
|
||||||
t.Run("Create", func(t *testing.T) {
|
|
||||||
hs := newServer()
|
hs := newServer()
|
||||||
adminPw := "admin"
|
// TODO: Add test
|
||||||
admin := &users.User{
|
username := "admin"
|
||||||
Username: "admin",
|
password := "admin"
|
||||||
Role: users.RoleAdmin,
|
user := &users.User{Username: username}
|
||||||
|
if err := user.SetPassword(password); err != nil {
|
||||||
|
t.Fatalf("Error setting user password: %s", err)
|
||||||
}
|
}
|
||||||
_ = admin.SetPassword(adminPw)
|
if err := hs.Users.Store(user); err != nil {
|
||||||
_ = hs.Users.Store(admin)
|
t.Fatalf("Error storing user: %s", err)
|
||||||
|
|
||||||
token, err := hs.Auth.Login(admin.Username, adminPw)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("error getting admin token: %s", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
requestData := &api.RequestAPIUserCreate{
|
requestData := struct {
|
||||||
Username: "test",
|
Username string `json:"username"`
|
||||||
Password: "test",
|
Password string `json:"password"`
|
||||||
|
}{
|
||||||
|
Username: username,
|
||||||
|
Password: password,
|
||||||
}
|
}
|
||||||
|
|
||||||
body := new(bytes.Buffer)
|
body := new(bytes.Buffer)
|
||||||
encoder := json.NewEncoder(body)
|
encoder := json.NewEncoder(body)
|
||||||
if err := encoder.Encode(requestData); err != nil {
|
if err := encoder.Encode(&requestData); err != nil {
|
||||||
t.Fatalf("Error encoding data: %s", err)
|
t.Fatalf("Error encoding request body: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
rr := httptest.NewRecorder()
|
rr := httptest.NewRecorder()
|
||||||
req := httptest.NewRequest(http.MethodPost, "/api/user", body)
|
req := httptest.NewRequest(http.MethodPost, "/api/login", body)
|
||||||
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
|
|
||||||
hs.Handler.ServeHTTP(rr, req)
|
hs.Handler.ServeHTTP(rr, req)
|
||||||
|
|
||||||
if rr.Result().StatusCode != http.StatusAccepted {
|
responseData := struct {
|
||||||
t.Fatalf("Create returned wrong status: %s", rr.Result().Status)
|
Token string `json:"token"`
|
||||||
|
}{}
|
||||||
|
|
||||||
|
decoder := json.NewDecoder(rr.Body)
|
||||||
|
if err := decoder.Decode(&responseData); err != nil {
|
||||||
|
t.Fatalf("Error decoding response: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
user, err := hs.Users.Get(requestData.Username)
|
if _, err := hs.Auth.ValidateToken(responseData.Token); err != nil {
|
||||||
if err != nil {
|
t.Fatalf("Unable to validate received token: %s", err)
|
||||||
t.Fatalf("Unable to get user after create: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
expectedUser := &users.User{
|
|
||||||
Username: requestData.Username,
|
|
||||||
Role: users.RoleUser,
|
|
||||||
}
|
|
||||||
ignorePW := cmp.FilterPath(func(p cmp.Path) bool {
|
|
||||||
return p.String() == "HashedPassword"
|
|
||||||
}, cmp.Ignore())
|
|
||||||
|
|
||||||
if diff := cmp.Diff(user, expectedUser, ignorePW); diff != "" {
|
|
||||||
t.Errorf("User does not match expected: %s", diff)
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func newServer() *api.HTTPServer {
|
func newServer() *api.HTTPServer {
|
||||||
|
Loading…
Reference in New Issue
Block a user