SimpleCloudNotifier/server/test/user_test.go

106 lines
2.7 KiB
Go
Raw Normal View History

2022-11-24 12:53:27 +01:00
package test
import (
"fmt"
"github.com/gin-gonic/gin"
"testing"
)
func TestCreateUserNoClient(t *testing.T) {
2022-11-30 10:35:05 +01:00
ws, stop := StartSimpleWebserver(t)
2022-11-24 12:53:27 +01:00
defer stop()
baseUrl := "http://127.0.0.1:" + ws.Port
2022-11-30 10:35:05 +01:00
r0 := requestPost[gin.H](t, baseUrl, "/api/users", gin.H{
2022-11-24 12:53:27 +01:00
"no_client": true,
})
2022-11-30 10:35:05 +01:00
assertEqual(t, "len(clients)", 0, len(r0["clients"].([]any)))
uid := fmt.Sprintf("%v", r0["user_id"])
admintok := r0["admin_key"].(string)
2022-11-24 12:53:27 +01:00
fmt.Printf("uid := %s\n", uid)
fmt.Printf("admin_key := %s\n", admintok)
2022-11-30 10:35:05 +01:00
r1 := requestAuthGet[gin.H](t, admintok, baseUrl, "/api/users/"+uid)
assertEqual(t, "uid", uid, fmt.Sprintf("%v", r1["user_id"]))
assertEqual(t, "admin_key", admintok, r1["admin_key"])
}
func TestCreateUserDummyClient(t *testing.T) {
ws, stop := StartSimpleWebserver(t)
defer stop()
baseUrl := "http://127.0.0.1:" + ws.Port
r0 := requestPost[gin.H](t, baseUrl, "/api/users", gin.H{
"agent_model": "DUMMY_PHONE",
"agent_version": "4X",
"client_type": "ANDROID",
"fcm_token": "DUMMY_FCM",
})
uid := fmt.Sprintf("%v", r0["user_id"])
assertEqual(t, "len(clients)", 1, len(r0["clients"].([]any)))
admintok := r0["admin_key"].(string)
fmt.Printf("uid := %s\n", uid)
fmt.Printf("admin_key := %s\n", admintok)
r1 := requestAuthGet[gin.H](t, admintok, baseUrl, "/api/users/"+uid)
assertEqual(t, "uid", uid, fmt.Sprintf("%v", r1["user_id"]))
assertEqual(t, "admin_key", admintok, r1["admin_key"])
assertEqual(t, "username", nil, r1["username"])
type rt2 struct {
Clients []gin.H `json:"clients"`
}
r2 := requestAuthGet[rt2](t, admintok, baseUrl, "/api/users/"+uid+"/clients")
assertEqual(t, "len(clients)", 1, len(r2.Clients))
c0 := r2.Clients[0]
assertEqual(t, "agent_model", "DUMMY_PHONE", c0["agent_model"])
assertEqual(t, "agent_version", "4X", c0["agent_version"])
assertEqual(t, "fcm_token", "DUMMY_FCM", c0["fcm_token"])
assertEqual(t, "client_type", "ANDROID", c0["type"])
}
func TestCreateUserWithUsername(t *testing.T) {
ws, stop := StartSimpleWebserver(t)
defer stop()
baseUrl := "http://127.0.0.1:" + ws.Port
r0 := requestPost[gin.H](t, baseUrl, "/api/users", gin.H{
"agent_model": "DUMMY_PHONE",
"agent_version": "4X",
"client_type": "ANDROID",
"fcm_token": "DUMMY_FCM",
"username": "my_user",
})
assertEqual(t, "len(clients)", 1, len(r0["clients"].([]any)))
uid := fmt.Sprintf("%v", r0["user_id"])
admintok := r0["admin_key"].(string)
fmt.Printf("uid := %s\n", uid)
fmt.Printf("admin_key := %s\n", admintok)
r1 := requestAuthGet[gin.H](t, admintok, baseUrl, "/api/users/"+uid)
assertEqual(t, "uid", uid, fmt.Sprintf("%v", r1["user_id"]))
assertEqual(t, "admin_key", admintok, r1["admin_key"])
assertEqual(t, "username", "my_user", r1["username"])
2022-11-24 12:53:27 +01:00
}