minimum viable (excluding auth)

This commit is contained in:
simon987
2019-03-09 09:20:51 -05:00
parent 5a7f3316e6
commit 6048cfbebc
11 changed files with 812 additions and 0 deletions

24
test/auth_test.go Normal file
View File

@@ -0,0 +1,24 @@
package test
import (
"github.com/simon987/ws_bucket/api"
"testing"
)
func TestCreateClient(t *testing.T) {
r := createClient(api.CreateClientRequest{
Alias: "testcreateclient",
})
if r.Ok != true {
t.Error()
}
}
func createClient(request api.CreateClientRequest) (ar *api.CreateClientResponse) {
resp := Post("/client", request)
UnmarshalResponse(resp, &ar)
return
}

63
test/common.go Normal file
View File

@@ -0,0 +1,63 @@
package test
import (
"bytes"
"encoding/json"
"fmt"
"github.com/simon987/ws_bucket/api"
"io/ioutil"
"net/http"
)
func Post(path string, x interface{}) *http.Response {
s := http.Client{}
body, err := json.Marshal(x)
buf := bytes.NewBuffer(body)
req, err := http.NewRequest("POST", "http://"+api.GetServerAddress()+path, buf)
handleErr(err)
//ts := time.Now().Format(time.RFC1123)
//
//mac := hmac.New(crypto.SHA256.New, worker.Secret)
//mac.Write(body)
//mac.Write([]byte(ts))
//sig := hex.EncodeToString(mac.Sum(nil))
//
//req.Header.Add("X-Worker-Id", strconv.FormatInt(worker.Id, 10))
//req.Header.Add("X-Signature", sig)
//req.Header.Add("Timestamp", ts)
r, err := s.Do(req)
handleErr(err)
return r
}
func Get(path string, token string) *http.Response {
s := http.Client{}
req, err := http.NewRequest("GET", "http://"+api.GetServerAddress()+path, nil)
handleErr(err)
req.Header.Set("X-Upload-Token", token)
r, err := s.Do(req)
return r
}
func UnmarshalResponse(r *http.Response, result interface{}) {
data, err := ioutil.ReadAll(r.Body)
fmt.Println(string(data))
err = json.Unmarshal(data, result)
handleErr(err)
}
func handleErr(err error) {
if err != nil {
panic(err)
}
}

25
test/main_test.go Normal file
View File

@@ -0,0 +1,25 @@
package test
import (
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"github.com/simon987/ws_bucket/api"
"testing"
"time"
)
func TestMain(m *testing.M) {
//db, err := gorm.Open("postgres", "host=localhost user=ws_bucket dbname=ws_bucket password=ws_bucket sslmode=disable")
db, err := gorm.Open("sqlite3", ":memory:")
if err != nil {
panic(err)
}
a := api.New(db)
go a.Run()
time.Sleep(time.Millisecond * 100)
m.Run()
}

53
test/slot_test.go Normal file
View File

@@ -0,0 +1,53 @@
package test
import (
"github.com/simon987/ws_bucket/api"
"testing"
)
func TestAllocateUploadInvalidMaxSize(t *testing.T) {
if allocateUploadSlot(api.AllocateUploadSlotRequest{
FileName: "valid",
Token: "valid",
MaxSize: -1,
}).Ok != false {
t.Error()
}
}
func TestAllocateUploadSlotInvalidToken(t *testing.T) {
if allocateUploadSlot(api.AllocateUploadSlotRequest{
FileName: "valid",
Token: "",
MaxSize: 100,
}).Ok != false {
t.Error()
}
}
func TestAllocateUploadSlotUnsafePath(t *testing.T) {
if allocateUploadSlot(api.AllocateUploadSlotRequest{
FileName: "../test.png",
Token: "valid",
MaxSize: 100,
}).Ok != false {
t.Error()
}
if allocateUploadSlot(api.AllocateUploadSlotRequest{
FileName: "test/../../test.png",
Token: "valid",
MaxSize: 100,
}).Ok != false {
t.Error()
}
}
func allocateUploadSlot(request api.AllocateUploadSlotRequest) (ar *api.GenericResponse) {
resp := Post("/slot", request)
UnmarshalResponse(resp, &ar)
return
}

179
test/upload_test.go Normal file
View File

@@ -0,0 +1,179 @@
package test
import (
"bytes"
"fmt"
"github.com/fasthttp/websocket"
"github.com/google/uuid"
"github.com/simon987/ws_bucket/api"
"io/ioutil"
"math"
"net/http"
"net/url"
"testing"
)
func TestWebsocketReturnsMotd(t *testing.T) {
id := uuid.New()
allocateUploadSlot(api.AllocateUploadSlotRequest{
FileName: "testmotd",
MaxSize: 0,
Token: id.String(),
})
c := ws(id.String())
motd := &api.WebsocketMotd{}
err := c.ReadJSON(&motd)
handleErr(err)
if len(motd.Motd) <= 0 {
t.Error()
}
if len(motd.Info.Version) <= 0 {
t.Error()
}
}
func TestWebSocketUploadSmallFile(t *testing.T) {
id := uuid.New()
allocateUploadSlot(api.AllocateUploadSlotRequest{
FileName: "testfile",
Token: id.String(),
MaxSize: math.MaxInt64,
})
c := ws(id.String())
_, _, err := c.ReadMessage()
handleErr(err)
err = c.WriteMessage(websocket.BinaryMessage, []byte("testuploadsmallfile"))
handleErr(err)
err = c.Close()
handleErr(err)
resp := readUploadSlot(id.String())
if bytes.Compare(resp, []byte("testuploadsmallfile")) != 0 {
t.Error()
}
}
func TestWebSocketUploadOverwritesFile(t *testing.T) {
id := uuid.New()
allocateUploadSlot(api.AllocateUploadSlotRequest{
FileName: "testuploadoverwrites",
Token: id.String(),
MaxSize: math.MaxInt64,
})
c := ws(id.String())
_, _, err := c.ReadMessage()
handleErr(err)
err = c.WriteMessage(websocket.BinaryMessage, []byte("testuploadsmallfile"))
handleErr(err)
err = c.Close()
handleErr(err)
c1 := ws(id.String())
_, _, err = c1.ReadMessage()
handleErr(err)
err = c1.WriteMessage(websocket.BinaryMessage, []byte("newvalue"))
handleErr(err)
err = c1.Close()
handleErr(err)
resp := readUploadSlot(id.String())
if bytes.Compare(resp, []byte("newvalue")) != 0 {
t.Error()
}
}
func TestWebSocketUploadLargeFile(t *testing.T) {
id := uuid.New()
allocateUploadSlot(api.AllocateUploadSlotRequest{
FileName: "testlargefile",
Token: id.String(),
MaxSize: math.MaxInt64,
})
c := ws(id.String())
_, _, err := c.ReadMessage()
handleErr(err)
chunk := make([]byte, 100000)
_ = copy(chunk, "test")
_ = c.WriteMessage(websocket.BinaryMessage, chunk)
err = c.Close()
handleErr(err)
resp := readUploadSlot(id.String())
if bytes.Compare(resp, chunk) != 0 {
t.Error()
}
}
func TestWebSocketUploadMaxSize(t *testing.T) {
id := uuid.New()
allocateUploadSlot(api.AllocateUploadSlotRequest{
FileName: "testmaxsize",
Token: id.String(),
MaxSize: 10,
})
c := ws(id.String())
_, _, err := c.ReadMessage()
handleErr(err)
chunk := make([]byte, 100000)
_ = copy(chunk, "test")
_ = c.WriteMessage(websocket.BinaryMessage, chunk)
err = c.Close()
handleErr(err)
resp := readUploadSlot(id.String())
if len(resp) != 10 {
t.Error()
}
}
func readUploadSlot(token string) []byte {
r := Get("/slot", token)
data, err := ioutil.ReadAll(r.Body)
handleErr(err)
return data
}
func ws(slot string) *websocket.Conn {
u := url.URL{Scheme: "ws", Host: "localhost:3021", Path: "/upload"}
fmt.Printf("Connecting to %s", u.String())
header := http.Header{}
header.Add("X-Upload-Token", slot)
c, _, err := websocket.DefaultDialer.Dial(u.String(), header)
handleErr(err)
return c
}