Added api endpoints and tests
This commit is contained in:
84
api/upload_test.go
Normal file
84
api/upload_test.go
Normal file
@@ -0,0 +1,84 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"TheAdversary/database"
|
||||
"TheAdversary/schema"
|
||||
"encoding/base64"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestUpload(t *testing.T) {
|
||||
if err := initTestDatabase("upload_test.sqlite3"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
database.GetDB().Table("article").Create([]map[string]interface{}{
|
||||
{
|
||||
"title": "Upload test",
|
||||
"summary": "An example article to test the upload api endpoint",
|
||||
"created": time.Now().Unix(),
|
||||
"link": "/article/upload-test",
|
||||
"markdown": "Oh god i have to test all this, what am i doing with my life",
|
||||
"html": "<p>Oh god i have to test all this, what am i doing with my life<p>",
|
||||
},
|
||||
})
|
||||
database.GetDB().Table("author").Create([]map[string]interface{}{
|
||||
{
|
||||
"name": "me",
|
||||
"password": "",
|
||||
"information": "this is my account",
|
||||
},
|
||||
})
|
||||
database.GetDB().Table("article_author").Create([]map[string]interface{}{
|
||||
{
|
||||
"article_id": 1,
|
||||
"author_id": 1,
|
||||
},
|
||||
})
|
||||
|
||||
server := httptest.NewServer(http.HandlerFunc(Upload))
|
||||
checkTestInformation(t, server.URL, []testInformation{
|
||||
{
|
||||
Method: http.MethodPost,
|
||||
Code: http.StatusUnauthorized,
|
||||
},
|
||||
{
|
||||
Method: http.MethodPost,
|
||||
Cookie: map[string]string{
|
||||
"session_id": initSession(),
|
||||
},
|
||||
Body: uploadPayload{
|
||||
Title: "Testooo",
|
||||
Summary: "I have no idea what to put in here",
|
||||
Authors: []int{1},
|
||||
Link: "testooo",
|
||||
Content: base64.StdEncoding.EncodeToString([]byte("### Testo")),
|
||||
},
|
||||
ResultBody: schema.ArticleSummary{
|
||||
Id: 2,
|
||||
Title: "Testooo",
|
||||
Summary: "I have no idea what to put in here",
|
||||
Authors: []schema.Author{
|
||||
{
|
||||
Id: 1,
|
||||
Name: "me",
|
||||
Information: "this is my account",
|
||||
},
|
||||
},
|
||||
Tags: []string{},
|
||||
Link: "/article/testooo",
|
||||
},
|
||||
Code: http.StatusCreated,
|
||||
AfterExec: func(information *testInformation) {
|
||||
var created int64
|
||||
database.GetDB().Table("article").Select("created").Where("id = 2").Find(&created)
|
||||
res := information.ResultBody.(schema.ArticleSummary)
|
||||
res.Created = created
|
||||
information.ResultBody = res
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user