2017-06-22 14:08:34 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-10-01 22:31:44 +00:00
|
|
|
"errors"
|
2017-06-22 14:08:34 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2017-10-01 22:31:44 +00:00
|
|
|
"reflect"
|
2017-06-22 14:08:34 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/aerogo/aero"
|
2017-10-01 22:31:44 +00:00
|
|
|
"github.com/aerogo/api"
|
|
|
|
"github.com/animenotifier/arn"
|
|
|
|
"github.com/fatih/color"
|
2017-06-22 14:08:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRoutes(t *testing.T) {
|
|
|
|
app := configure(aero.New())
|
|
|
|
|
2017-07-02 15:51:17 +00:00
|
|
|
for _, examples := range routeTests {
|
2017-06-25 10:05:32 +00:00
|
|
|
for _, example := range examples {
|
|
|
|
request, err := http.NewRequest("GET", example, nil)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-06-22 14:08:34 +00:00
|
|
|
|
2017-06-25 10:05:32 +00:00
|
|
|
responseRecorder := httptest.NewRecorder()
|
|
|
|
app.Handler().ServeHTTP(responseRecorder, request)
|
2017-06-22 14:08:34 +00:00
|
|
|
|
2017-06-25 10:05:32 +00:00
|
|
|
if status := responseRecorder.Code; status != http.StatusOK {
|
2017-10-01 22:31:44 +00:00
|
|
|
color.Red("%s | Wrong status code | %v instead of %v", example, status, http.StatusOK)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInterfaceImplementations(t *testing.T) {
|
|
|
|
// API interfaces
|
|
|
|
var creatable = reflect.TypeOf((*api.Creatable)(nil)).Elem()
|
2017-10-11 08:37:33 +00:00
|
|
|
var editable = reflect.TypeOf((*api.Editable)(nil)).Elem()
|
2017-10-01 22:31:44 +00:00
|
|
|
var actionable = reflect.TypeOf((*api.Actionable)(nil)).Elem()
|
|
|
|
var collection = reflect.TypeOf((*api.Collection)(nil)).Elem()
|
|
|
|
|
|
|
|
// Required interface implementations
|
|
|
|
var interfaceImplementations = map[string][]reflect.Type{
|
|
|
|
"User": []reflect.Type{
|
2017-10-11 08:37:33 +00:00
|
|
|
editable,
|
2017-10-01 22:31:44 +00:00
|
|
|
},
|
|
|
|
"Thread": []reflect.Type{
|
|
|
|
creatable,
|
2017-10-11 08:37:33 +00:00
|
|
|
editable,
|
2017-10-01 22:31:44 +00:00
|
|
|
actionable,
|
|
|
|
},
|
|
|
|
"Post": []reflect.Type{
|
|
|
|
creatable,
|
2017-10-11 08:37:33 +00:00
|
|
|
editable,
|
2017-10-01 22:31:44 +00:00
|
|
|
actionable,
|
|
|
|
},
|
|
|
|
"SoundTrack": []reflect.Type{
|
|
|
|
creatable,
|
2017-10-11 10:45:04 +00:00
|
|
|
editable,
|
2017-10-01 22:31:44 +00:00
|
|
|
},
|
|
|
|
"Analytics": []reflect.Type{
|
|
|
|
creatable,
|
|
|
|
},
|
|
|
|
"AnimeList": []reflect.Type{
|
|
|
|
collection,
|
|
|
|
},
|
|
|
|
"PushSubscriptions": []reflect.Type{
|
|
|
|
collection,
|
|
|
|
},
|
|
|
|
"UserFollows": []reflect.Type{
|
|
|
|
collection,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for typeName, interfaces := range interfaceImplementations {
|
|
|
|
for _, requiredInterface := range interfaces {
|
|
|
|
if !reflect.PtrTo(arn.DB.Type(typeName)).Implements(requiredInterface) {
|
|
|
|
panic(errors.New(typeName + " does not implement interface " + requiredInterface.Name()))
|
2017-06-25 10:05:32 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-22 14:08:34 +00:00
|
|
|
}
|
|
|
|
}
|