Tests[RequestLogSimple]
This commit is contained in:
56
scnserver/test/keytoken_test.go
Normal file
56
scnserver/test/keytoken_test.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
tt "blackforestbytes.com/simplecloudnotifier/test/util"
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestListUserKeys(t *testing.T) {
|
||||
ws, baseUrl, stop := tt.StartSimpleWebserver(t)
|
||||
defer stop()
|
||||
|
||||
data := tt.InitSingleData(t, ws)
|
||||
|
||||
type keylist struct {
|
||||
Tokens []struct {
|
||||
AllChannels bool `json:"all_channels"`
|
||||
Channels []string `json:"channels"`
|
||||
KeytokenId string `json:"keytoken_id"`
|
||||
MessagesSent int `json:"messages_sent"`
|
||||
Name string `json:"name"`
|
||||
OwnerUserId string `json:"owner_user_id"`
|
||||
Permissions string `json:"permissions"`
|
||||
} `json:"tokens"`
|
||||
}
|
||||
|
||||
klist := tt.RequestAuthGet[keylist](t, data.AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/keys", data.UserID))
|
||||
|
||||
tt.AssertEqual(t, "len(keys)", 1, len(klist.Tokens))
|
||||
|
||||
t.SkipNow() //TODO
|
||||
}
|
||||
|
||||
func TestCreateUserKey(t *testing.T) {
|
||||
t.SkipNow() //TODO
|
||||
}
|
||||
|
||||
func TestDeleteUserKey(t *testing.T) {
|
||||
t.SkipNow() //TODO
|
||||
}
|
||||
|
||||
func TestGetUserKey(t *testing.T) {
|
||||
t.SkipNow() //TODO
|
||||
}
|
||||
|
||||
func TestUpdateUserKey(t *testing.T) {
|
||||
t.SkipNow() //TODO
|
||||
}
|
||||
|
||||
func TestUserKeyPermissions(t *testing.T) {
|
||||
t.SkipNow() //TODO
|
||||
}
|
||||
|
||||
func TestUsedKeyInMessage(t *testing.T) {
|
||||
t.SkipNow() //TODO
|
||||
}
|
@@ -1,3 +1,126 @@
|
||||
package test
|
||||
|
||||
//TODO test requestlog
|
||||
import (
|
||||
ct "blackforestbytes.com/simplecloudnotifier/db/cursortoken"
|
||||
"blackforestbytes.com/simplecloudnotifier/models"
|
||||
tt "blackforestbytes.com/simplecloudnotifier/test/util"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/url"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestRequestLogSimple(t *testing.T) {
|
||||
ws, baseUrl, stop := tt.StartSimpleWebserver(t)
|
||||
defer stop()
|
||||
|
||||
ctx := ws.NewSimpleTransactionContext(5 * time.Second)
|
||||
defer ctx.Cancel()
|
||||
|
||||
// Ping
|
||||
{
|
||||
tt.RequestGet[tt.Void](t, baseUrl, fmt.Sprintf("/api/ping"))
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
rl, _, err := ws.Database.Requests.ListRequestLogs(ctx, models.RequestLogFilter{}, nil, ct.Start())
|
||||
tt.TestFailIfErr(t, err)
|
||||
|
||||
tt.AssertEqual(t, "requestlog.count", 1, len(rl))
|
||||
|
||||
tt.AssertEqual(t, "requestlog[0].Method", "GET", rl[0].Method)
|
||||
tt.AssertEqual(t, "requestlog[0].KeyID", nil, rl[0].KeyID)
|
||||
tt.AssertEqual(t, "requestlog[0].UserID", nil, rl[0].UserID)
|
||||
tt.AssertEqual(t, "requestlog[0].Panicked", false, rl[0].Panicked)
|
||||
tt.AssertEqual(t, "requestlog[0].URI", "/api/ping", rl[0].URI)
|
||||
tt.AssertEqual(t, "requestlog[0].ResponseContentType", "application/json", rl[0].ResponseContentType)
|
||||
}
|
||||
|
||||
// HTMl request
|
||||
{
|
||||
tt.RequestRaw(t, baseUrl, fmt.Sprintf("/"))
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
rl, _, err := ws.Database.Requests.ListRequestLogs(ctx, models.RequestLogFilter{}, nil, ct.Start())
|
||||
tt.TestFailIfErr(t, err)
|
||||
|
||||
tt.AssertEqual(t, "requestlog.count", 2, len(rl))
|
||||
|
||||
tt.AssertEqual(t, "requestlog[0].Method", "GET", rl[0].Method)
|
||||
tt.AssertEqual(t, "requestlog[0].KeyID", nil, rl[0].KeyID)
|
||||
tt.AssertEqual(t, "requestlog[0].UserID", nil, rl[0].UserID)
|
||||
tt.AssertEqual(t, "requestlog[0].Panicked", false, rl[0].Panicked)
|
||||
tt.AssertEqual(t, "requestlog[0].URI", "/", rl[0].URI)
|
||||
tt.AssertEqual(t, "requestlog[0].ResponseContentType", "text/html", rl[0].ResponseContentType)
|
||||
}
|
||||
|
||||
type R struct {
|
||||
Clients []struct {
|
||||
ClientId string `json:"client_id"`
|
||||
UserId string `json:"user_id"`
|
||||
} `json:"clients"`
|
||||
ReadKey string `json:"read_key"`
|
||||
SendKey string `json:"send_key"`
|
||||
AdminKey string `json:"admin_key"`
|
||||
UserId string `json:"user_id"`
|
||||
}
|
||||
usr := tt.RequestPost[R](t, baseUrl, "/api/v2/users", gin.H{
|
||||
"agent_model": "DUMMY_PHONE",
|
||||
"agent_version": "4X",
|
||||
"client_type": "ANDROID",
|
||||
"fcm_token": "DUMMY_FCM",
|
||||
})
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
// API request
|
||||
{
|
||||
|
||||
tt.RequestAuthGet[R](t, usr.AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s", usr.UserId))
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
rl, _, err := ws.Database.Requests.ListRequestLogs(ctx, models.RequestLogFilter{}, nil, ct.Start())
|
||||
tt.TestFailIfErr(t, err)
|
||||
|
||||
tt.AssertEqual(t, "requestlog.count", 4, len(rl))
|
||||
|
||||
tt.AssertEqual(t, "requestlog[0].Method", "GET", rl[0].Method)
|
||||
tt.AssertNotEqual(t, "requestlog[0].KeyID", nil, rl[0].KeyID)
|
||||
tt.AssertStrRepEqual(t, "requestlog[0].UserID", usr.UserId, rl[0].UserID)
|
||||
tt.AssertEqual(t, "requestlog[0].Panicked", false, rl[0].Panicked)
|
||||
tt.AssertStrRepEqual(t, "requestlog[0].Permissions", "A", rl[0].Permissions)
|
||||
tt.AssertEqual(t, "requestlog[0].URI", fmt.Sprintf("/api/v2/users/%s", usr.UserId), rl[0].URI)
|
||||
tt.AssertEqual(t, "requestlog[0].ResponseContentType", "application/json", rl[0].ResponseContentType)
|
||||
}
|
||||
|
||||
// Send request
|
||||
{
|
||||
tt.RequestPost[gin.H](t, baseUrl, fmt.Sprintf("/?user_id=%s&key=%s&title=%s", usr.UserId, usr.SendKey, url.QueryEscape("Hello World 2134")), nil)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
rl, _, err := ws.Database.Requests.ListRequestLogs(ctx, models.RequestLogFilter{}, nil, ct.Start())
|
||||
tt.TestFailIfErr(t, err)
|
||||
|
||||
tt.AssertEqual(t, "requestlog.count", 5, len(rl))
|
||||
|
||||
tt.AssertEqual(t, "requestlog[0].Method", "POST", rl[0].Method)
|
||||
tt.AssertEqual(t, "requestlog[0].UserID", nil, rl[0].UserID)
|
||||
tt.AssertEqual(t, "requestlog[0].ResponseContentType", "application/json", rl[0].ResponseContentType)
|
||||
}
|
||||
|
||||
// Failed request
|
||||
{
|
||||
tt.RequestAuthGetShouldFail(t, usr.AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s", models.NewUserID()), 0, 0)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
rl, _, err := ws.Database.Requests.ListRequestLogs(ctx, models.RequestLogFilter{}, nil, ct.Start())
|
||||
tt.TestFailIfErr(t, err)
|
||||
|
||||
tt.AssertEqual(t, "requestlog.count", 6, len(rl))
|
||||
|
||||
tt.AssertEqual(t, "requestlog[0].Method", "GET", rl[0].Method)
|
||||
tt.AssertStrRepEqual(t, "requestlog[0].UserID", usr.UserId, rl[0].UserID)
|
||||
tt.AssertEqual(t, "requestlog[0].ResponseContentType", "application/json", rl[0].ResponseContentType)
|
||||
tt.AssertStrRepEqual(t, "requestlog[0].ResponseStatuscode", 401, rl[0].ResponseStatuscode)
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -140,6 +140,10 @@ func AssertEqual(t *testing.T, key string, expected any, actual any) {
|
||||
|
||||
}
|
||||
|
||||
if langext.IsNil(expected) && langext.IsNil(actual) {
|
||||
return
|
||||
}
|
||||
|
||||
if expected != actual {
|
||||
t.Errorf("Value [%s] differs (%T <-> %T):\n", key, expected, actual)
|
||||
|
||||
@@ -173,7 +177,7 @@ func AssertTrue(t *testing.T, key string, v bool) {
|
||||
}
|
||||
|
||||
func AssertNotEqual(t *testing.T, key string, expected any, actual any) {
|
||||
if expected == actual {
|
||||
if expected == actual || (langext.IsNil(expected) && langext.IsNil(actual)) {
|
||||
t.Errorf("Value [%s] does not differ (%T <-> %T):\n", key, expected, actual)
|
||||
|
||||
str1 := fmt.Sprintf("%v", expected)
|
||||
|
@@ -395,6 +395,57 @@ func InitDefaultData(t *testing.T, ws *logic.Application) DefData {
|
||||
return DefData{User: users}
|
||||
}
|
||||
|
||||
type SingleData struct {
|
||||
UserID string
|
||||
AdminKey string
|
||||
SendKey string
|
||||
ReadKey string
|
||||
ClientID string
|
||||
}
|
||||
|
||||
func InitSingleData(t *testing.T, ws *logic.Application) SingleData {
|
||||
|
||||
// set logger to buffer, only output if error occured
|
||||
success := false
|
||||
SetBufLogger()
|
||||
defer func() {
|
||||
ClearBufLogger(!success)
|
||||
if success {
|
||||
log.Info().Msgf("Succesfully initialized default data (%d messages, %d users)", len(messageExamples), len(userExamples))
|
||||
}
|
||||
}()
|
||||
|
||||
baseUrl := "http://127.0.0.1:" + ws.Port
|
||||
|
||||
type R struct {
|
||||
Clients []struct {
|
||||
ClientId string `json:"client_id"`
|
||||
UserId string `json:"user_id"`
|
||||
} `json:"clients"`
|
||||
ReadKey string `json:"read_key"`
|
||||
SendKey string `json:"send_key"`
|
||||
AdminKey string `json:"admin_key"`
|
||||
UserId string `json:"user_id"`
|
||||
}
|
||||
|
||||
r0 := RequestPost[R](t, baseUrl, "/api/v2/users", gin.H{
|
||||
"agent_model": "DUMMY_PHONE",
|
||||
"agent_version": "4X",
|
||||
"client_type": "ANDROID",
|
||||
"fcm_token": "DUMMY_FCM",
|
||||
})
|
||||
|
||||
success = true
|
||||
|
||||
return SingleData{
|
||||
UserID: r0.UserId,
|
||||
AdminKey: r0.AdminKey,
|
||||
SendKey: r0.SendKey,
|
||||
ReadKey: r0.ReadKey,
|
||||
ClientID: r0.Clients[0].ClientId,
|
||||
}
|
||||
}
|
||||
|
||||
func doSubscribe(t *testing.T, baseUrl string, user Userdat, chanOwner Userdat, chanInternalName string) {
|
||||
|
||||
if user == chanOwner {
|
||||
|
@@ -14,44 +14,48 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func RequestRaw(t *testing.T, baseURL string, urlSuffix string) {
|
||||
RequestAny[Void](t, "", "GET", baseURL, urlSuffix, nil, false)
|
||||
}
|
||||
|
||||
func RequestGet[TResult any](t *testing.T, baseURL string, urlSuffix string) TResult {
|
||||
return RequestAny[TResult](t, "", "GET", baseURL, urlSuffix, nil)
|
||||
return RequestAny[TResult](t, "", "GET", baseURL, urlSuffix, nil, true)
|
||||
}
|
||||
|
||||
func RequestAuthGet[TResult any](t *testing.T, akey string, baseURL string, urlSuffix string) TResult {
|
||||
return RequestAny[TResult](t, akey, "GET", baseURL, urlSuffix, nil)
|
||||
return RequestAny[TResult](t, akey, "GET", baseURL, urlSuffix, nil, true)
|
||||
}
|
||||
|
||||
func RequestPost[TResult any](t *testing.T, baseURL string, urlSuffix string, body any) TResult {
|
||||
return RequestAny[TResult](t, "", "POST", baseURL, urlSuffix, body)
|
||||
return RequestAny[TResult](t, "", "POST", baseURL, urlSuffix, body, true)
|
||||
}
|
||||
|
||||
func RequestAuthPost[TResult any](t *testing.T, akey string, baseURL string, urlSuffix string, body any) TResult {
|
||||
return RequestAny[TResult](t, akey, "POST", baseURL, urlSuffix, body)
|
||||
return RequestAny[TResult](t, akey, "POST", baseURL, urlSuffix, body, true)
|
||||
}
|
||||
|
||||
func RequestPut[TResult any](t *testing.T, baseURL string, urlSuffix string, body any) TResult {
|
||||
return RequestAny[TResult](t, "", "PUT", baseURL, urlSuffix, body)
|
||||
return RequestAny[TResult](t, "", "PUT", baseURL, urlSuffix, body, true)
|
||||
}
|
||||
|
||||
func RequestAuthPUT[TResult any](t *testing.T, akey string, baseURL string, urlSuffix string, body any) TResult {
|
||||
return RequestAny[TResult](t, akey, "PUT", baseURL, urlSuffix, body)
|
||||
return RequestAny[TResult](t, akey, "PUT", baseURL, urlSuffix, body, true)
|
||||
}
|
||||
|
||||
func RequestPatch[TResult any](t *testing.T, baseURL string, urlSuffix string, body any) TResult {
|
||||
return RequestAny[TResult](t, "", "PATCH", baseURL, urlSuffix, body)
|
||||
return RequestAny[TResult](t, "", "PATCH", baseURL, urlSuffix, body, true)
|
||||
}
|
||||
|
||||
func RequestAuthPatch[TResult any](t *testing.T, akey string, baseURL string, urlSuffix string, body any) TResult {
|
||||
return RequestAny[TResult](t, akey, "PATCH", baseURL, urlSuffix, body)
|
||||
return RequestAny[TResult](t, akey, "PATCH", baseURL, urlSuffix, body, true)
|
||||
}
|
||||
|
||||
func RequestDelete[TResult any](t *testing.T, baseURL string, urlSuffix string, body any) TResult {
|
||||
return RequestAny[TResult](t, "", "DELETE", baseURL, urlSuffix, body)
|
||||
return RequestAny[TResult](t, "", "DELETE", baseURL, urlSuffix, body, true)
|
||||
}
|
||||
|
||||
func RequestAuthDelete[TResult any](t *testing.T, akey string, baseURL string, urlSuffix string, body any) TResult {
|
||||
return RequestAny[TResult](t, akey, "DELETE", baseURL, urlSuffix, body)
|
||||
return RequestAny[TResult](t, akey, "DELETE", baseURL, urlSuffix, body, true)
|
||||
}
|
||||
|
||||
func RequestGetShouldFail(t *testing.T, baseURL string, urlSuffix string, statusCode int, errcode apierr.APIError) {
|
||||
@@ -86,7 +90,7 @@ func RequestAuthDeleteShouldFail(t *testing.T, akey string, baseURL string, urlS
|
||||
RequestAuthAnyShouldFail(t, akey, "DELETE", baseURL, urlSuffix, body, statusCode, errcode)
|
||||
}
|
||||
|
||||
func RequestAny[TResult any](t *testing.T, akey string, method string, baseURL string, urlSuffix string, body any) TResult {
|
||||
func RequestAny[TResult any](t *testing.T, akey string, method string, baseURL string, urlSuffix string, body any, deserialize bool) TResult {
|
||||
client := http.Client{}
|
||||
|
||||
TPrintf("[-> REQUEST] (%s) %s%s [%s] [%s]\n", method, baseURL, urlSuffix, langext.Conditional(akey == "", "NO AUTH", "AUTH"), langext.Conditional(body == nil, "NO BODY", "BODY"))
|
||||
@@ -156,8 +160,10 @@ func RequestAny[TResult any](t *testing.T, akey string, method string, baseURL s
|
||||
}
|
||||
|
||||
var data TResult
|
||||
if err := json.Unmarshal(respBodyBin, &data); err != nil {
|
||||
TestFailErr(t, err)
|
||||
if deserialize {
|
||||
if err := json.Unmarshal(respBodyBin, &data); err != nil {
|
||||
TestFailErr(t, err)
|
||||
}
|
||||
}
|
||||
|
||||
return data
|
||||
|
Reference in New Issue
Block a user