2022-11-30 12:40:03 +01:00
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
2022-11-30 13:57:55 +01:00
|
|
|
tt "blackforestbytes.com/simplecloudnotifier/test/util"
|
2022-11-30 12:40:03 +01:00
|
|
|
"fmt"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetClient(t *testing.T) {
|
2022-11-30 13:57:55 +01:00
|
|
|
ws, stop := tt.StartSimpleWebserver(t)
|
2022-11-30 12:40:03 +01:00
|
|
|
defer stop()
|
|
|
|
|
|
|
|
baseUrl := "http://127.0.0.1:" + ws.Port
|
|
|
|
|
2022-11-30 13:57:55 +01:00
|
|
|
r0 := tt.RequestPost[gin.H](t, baseUrl, "/api/users", gin.H{
|
2022-11-30 12:40:03 +01:00
|
|
|
"agent_model": "DUMMY_PHONE",
|
|
|
|
"agent_version": "4X",
|
|
|
|
"client_type": "ANDROID",
|
|
|
|
"fcm_token": "DUMMY_FCM",
|
|
|
|
})
|
|
|
|
|
|
|
|
uid := fmt.Sprintf("%v", r0["user_id"])
|
|
|
|
|
2022-11-30 13:57:55 +01:00
|
|
|
tt.AssertEqual(t, "len(clients)", 1, len(r0["clients"].([]any)))
|
2022-11-30 12:40:03 +01:00
|
|
|
|
|
|
|
admintok := r0["admin_key"].(string)
|
|
|
|
|
|
|
|
fmt.Printf("uid := %s\n", uid)
|
|
|
|
fmt.Printf("admin_key := %s\n", admintok)
|
|
|
|
|
2022-11-30 13:57:55 +01:00
|
|
|
r1 := tt.RequestAuthGet[gin.H](t, admintok, baseUrl, "/api/users/"+uid)
|
2022-11-30 12:40:03 +01:00
|
|
|
|
2022-11-30 13:57:55 +01:00
|
|
|
tt.AssertEqual(t, "uid", uid, fmt.Sprintf("%v", r1["user_id"]))
|
|
|
|
tt.AssertEqual(t, "admin_key", admintok, r1["admin_key"])
|
|
|
|
tt.AssertEqual(t, "username", nil, r1["username"])
|
2022-11-30 12:40:03 +01:00
|
|
|
|
|
|
|
type rt2 struct {
|
|
|
|
Clients []gin.H `json:"clients"`
|
|
|
|
}
|
|
|
|
|
2022-11-30 13:57:55 +01:00
|
|
|
r2 := tt.RequestAuthGet[rt2](t, admintok, baseUrl, "/api/users/"+uid+"/clients")
|
2022-11-30 12:40:03 +01:00
|
|
|
|
2022-11-30 13:57:55 +01:00
|
|
|
tt.AssertEqual(t, "len(clients)", 1, len(r2.Clients))
|
2022-11-30 12:40:03 +01:00
|
|
|
|
|
|
|
c0 := r2.Clients[0]
|
|
|
|
|
2022-11-30 13:57:55 +01:00
|
|
|
tt.AssertEqual(t, "agent_model", "DUMMY_PHONE", c0["agent_model"])
|
|
|
|
tt.AssertEqual(t, "agent_version", "4X", c0["agent_version"])
|
|
|
|
tt.AssertEqual(t, "fcm_token", "DUMMY_FCM", c0["fcm_token"])
|
|
|
|
tt.AssertEqual(t, "client_type", "ANDROID", c0["type"])
|
|
|
|
tt.AssertEqual(t, "user_id", uid, fmt.Sprintf("%v", c0["user_id"]))
|
2022-11-30 12:40:03 +01:00
|
|
|
|
|
|
|
cid := fmt.Sprintf("%v", c0["client_id"])
|
|
|
|
|
2022-11-30 13:57:55 +01:00
|
|
|
r3 := tt.RequestAuthGet[gin.H](t, admintok, baseUrl, "/api/users/"+uid+"/clients/"+cid)
|
2022-11-30 12:40:03 +01:00
|
|
|
|
2022-11-30 13:57:55 +01:00
|
|
|
tt.AssertJsonMapEqual(t, "client", r3, c0)
|
2022-11-30 12:40:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateAndDeleteClient(t *testing.T) {
|
2022-11-30 13:57:55 +01:00
|
|
|
ws, stop := tt.StartSimpleWebserver(t)
|
2022-11-30 12:40:03 +01:00
|
|
|
defer stop()
|
|
|
|
|
|
|
|
baseUrl := "http://127.0.0.1:" + ws.Port
|
|
|
|
|
2022-11-30 13:57:55 +01:00
|
|
|
r0 := tt.RequestPost[gin.H](t, baseUrl, "/api/users", gin.H{
|
2022-11-30 12:40:03 +01:00
|
|
|
"agent_model": "DUMMY_PHONE",
|
|
|
|
"agent_version": "4X",
|
|
|
|
"client_type": "ANDROID",
|
|
|
|
"fcm_token": "DUMMY_FCM",
|
|
|
|
})
|
|
|
|
|
|
|
|
uid := fmt.Sprintf("%v", r0["user_id"])
|
|
|
|
|
2022-11-30 13:57:55 +01:00
|
|
|
tt.AssertEqual(t, "len(clients)", 1, len(r0["clients"].([]any)))
|
2022-11-30 12:40:03 +01:00
|
|
|
|
|
|
|
admintok := r0["admin_key"].(string)
|
|
|
|
|
|
|
|
fmt.Printf("uid := %s\n", uid)
|
|
|
|
fmt.Printf("admin_key := %s\n", admintok)
|
|
|
|
|
2022-11-30 13:57:55 +01:00
|
|
|
r2 := tt.RequestAuthPost[gin.H](t, admintok, baseUrl, "/api/users/"+uid+"/clients", gin.H{
|
2022-11-30 12:40:03 +01:00
|
|
|
"agent_model": "DUMMY_PHONE_2",
|
|
|
|
"agent_version": "99X",
|
|
|
|
"client_type": "IOS",
|
|
|
|
"fcm_token": "DUMMY_FCM_2",
|
|
|
|
})
|
|
|
|
|
|
|
|
cid2 := fmt.Sprintf("%v", r2["client_id"])
|
|
|
|
|
|
|
|
type rt3 struct {
|
|
|
|
Clients []gin.H `json:"clients"`
|
|
|
|
}
|
|
|
|
|
2022-11-30 13:57:55 +01:00
|
|
|
r3 := tt.RequestAuthGet[rt3](t, admintok, baseUrl, "/api/users/"+uid+"/clients")
|
|
|
|
tt.AssertEqual(t, "len(clients)", 2, len(r3.Clients))
|
2022-11-30 12:40:03 +01:00
|
|
|
|
2022-11-30 13:57:55 +01:00
|
|
|
r4 := tt.RequestAuthDelete[gin.H](t, admintok, baseUrl, "/api/users/"+uid+"/clients/"+cid2, nil)
|
|
|
|
tt.AssertEqual(t, "client_id", cid2, fmt.Sprintf("%v", r4["client_id"]))
|
2022-11-30 12:40:03 +01:00
|
|
|
|
2022-11-30 13:57:55 +01:00
|
|
|
r5 := tt.RequestAuthGet[rt3](t, admintok, baseUrl, "/api/users/"+uid+"/clients")
|
|
|
|
tt.AssertEqual(t, "len(clients)", 1, len(r5.Clients))
|
2022-11-30 12:40:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestReuseFCM(t *testing.T) {
|
2022-11-30 13:57:55 +01:00
|
|
|
ws, stop := tt.StartSimpleWebserver(t)
|
2022-11-30 12:40:03 +01:00
|
|
|
defer stop()
|
|
|
|
|
|
|
|
baseUrl := "http://127.0.0.1:" + ws.Port
|
|
|
|
|
2022-11-30 13:57:55 +01:00
|
|
|
r0 := tt.RequestPost[gin.H](t, baseUrl, "/api/users", gin.H{
|
2022-11-30 12:40:03 +01:00
|
|
|
"agent_model": "DUMMY_PHONE",
|
|
|
|
"agent_version": "4X",
|
|
|
|
"client_type": "ANDROID",
|
|
|
|
"fcm_token": "DUMMY_FCM_001",
|
|
|
|
})
|
|
|
|
|
|
|
|
uid := fmt.Sprintf("%v", r0["user_id"])
|
|
|
|
|
2022-11-30 13:57:55 +01:00
|
|
|
tt.AssertEqual(t, "len(clients)", 1, len(r0["clients"].([]any)))
|
2022-11-30 12:40:03 +01:00
|
|
|
|
|
|
|
admintok := r0["admin_key"].(string)
|
|
|
|
|
|
|
|
fmt.Printf("uid := %s\n", uid)
|
|
|
|
fmt.Printf("admin_key := %s\n", admintok)
|
|
|
|
|
|
|
|
type rt2 struct {
|
|
|
|
Clients []gin.H `json:"clients"`
|
|
|
|
}
|
|
|
|
|
2022-11-30 13:57:55 +01:00
|
|
|
r1 := tt.RequestAuthGet[rt2](t, admintok, baseUrl, "/api/users/"+uid+"/clients")
|
2022-11-30 12:40:03 +01:00
|
|
|
|
2022-11-30 13:57:55 +01:00
|
|
|
tt.AssertEqual(t, "len(clients)", 1, len(r1.Clients))
|
2022-11-30 12:40:03 +01:00
|
|
|
|
2022-11-30 13:57:55 +01:00
|
|
|
r2 := tt.RequestAuthPost[gin.H](t, admintok, baseUrl, "/api/users/"+uid+"/clients", gin.H{
|
2022-11-30 12:40:03 +01:00
|
|
|
"agent_model": "DUMMY_PHONE_2",
|
|
|
|
"agent_version": "99X",
|
|
|
|
"client_type": "IOS",
|
|
|
|
"fcm_token": "DUMMY_FCM_001",
|
|
|
|
})
|
|
|
|
|
|
|
|
cid2 := fmt.Sprintf("%v", r2["client_id"])
|
|
|
|
|
|
|
|
type rt3 struct {
|
|
|
|
Clients []gin.H `json:"clients"`
|
|
|
|
}
|
|
|
|
|
2022-11-30 13:57:55 +01:00
|
|
|
r3 := tt.RequestAuthGet[rt3](t, admintok, baseUrl, "/api/users/"+uid+"/clients")
|
|
|
|
tt.AssertEqual(t, "len(clients)", 1, len(r3.Clients))
|
2022-11-30 12:40:03 +01:00
|
|
|
|
2022-11-30 13:57:55 +01:00
|
|
|
tt.AssertEqual(t, "clients->client_id", cid2, fmt.Sprintf("%v", r3.Clients[0]["client_id"]))
|
2022-11-30 12:40:03 +01:00
|
|
|
}
|
2022-12-09 00:13:10 +01:00
|
|
|
|
|
|
|
//TODO test missing client-xx methods
|