package api import ( "TheAdversary/database" "TheAdversary/schema" "encoding/base64" "net/http" "net/http/httptest" "testing" ) func TestAssetsGet(t *testing.T) { if err := initTestDatabase("assets_get_test.sqlite3"); err != nil { t.Fatal(err) } database.GetDB().Table("assets").Create([]map[string]interface{}{ { "name": "linux", "data": "this should be an image of tux :3", "link": "/assets/linux", }, { "name": "get test", "data": "", "link": "/assets/get-test", }, }) server := httptest.NewServer(http.HandlerFunc(assetsGet)) checkTestInformation(t, server.URL, []testInformation{ { Method: http.MethodGet, Query: map[string]interface{}{ "q": "linux", }, ResultBody: []schema.Asset{ { Id: 1, Name: "linux", Link: "/assets/linux", }, }, Code: http.StatusOK, }, { Method: http.MethodGet, Query: map[string]interface{}{ "limit": 1, }, ResultBody: []schema.Asset{ { Id: 1, Name: "linux", Link: "/assets/linux", }, }, Code: http.StatusOK, }, { Method: http.MethodGet, ResultBody: []schema.Asset{ { Id: 1, Name: "linux", Link: "/assets/linux", }, { Id: 2, Name: "get test", Link: "/assets/get-test", }, }, Code: http.StatusOK, }, }) } func TestAssetsPost(t *testing.T) { if err := initTestDatabase("assets_post_test.sqlite3"); err != nil { t.Fatal(err) } server := httptest.NewServer(http.HandlerFunc(assetsPost)) checkTestInformation(t, server.URL, []testInformation{ { Method: http.MethodPost, Body: assetsPostPayload{ Name: "test", Content: base64.StdEncoding.EncodeToString([]byte("test asset")), }, ResultBody: schema.Asset{ Id: 1, Name: "test", Link: "/assets/test", }, Code: http.StatusOK, }, { Method: http.MethodPost, Body: assetsPostPayload{ Name: "test", Content: base64.StdEncoding.EncodeToString([]byte("test asset")), }, Code: http.StatusConflict, }, }) } func TestAssetsDelete(t *testing.T) { if err := initTestDatabase("assets_delete_test.sqlite3"); err != nil { t.Fatal(err) } database.GetDB().Table("assets").Create(map[string]interface{}{ "name": "example", "data": "just a normal string", "link": "/assets/example", }) server := httptest.NewServer(http.HandlerFunc(assetsDelete)) checkTestInformation(t, server.URL, []testInformation{ { Method: http.MethodDelete, Body: assetsDeletePayload{ Id: 1, }, Code: http.StatusOK, }, { Method: http.MethodDelete, Body: assetsDeletePayload{ Id: 69, }, Code: http.StatusNotFound, }, }) }