11package api
22
33import (
4+ "bytes"
5+ "encoding/json"
6+ "net/http"
7+ "net/http/httptest"
48 "testing"
59
10+ "github.com/labstack/echo/v4"
11+ "github.com/nethesis/matrix2acrobits/models"
12+ "github.com/nethesis/matrix2acrobits/service"
613 "github.com/stretchr/testify/assert"
714)
815
@@ -28,3 +35,64 @@ func TestIsLocalhost(t *testing.T) {
2835 })
2936 }
3037}
38+
39+ func TestPushTokenReport (t * testing.T ) {
40+ e := echo .New ()
41+ svc := service .NewMessageService (nil , nil )
42+
43+ t .Run ("valid push token report" , func (t * testing.T ) {
44+ reqBody := models.PushTokenReportRequest {
45+ Selector : "12869E0E6E553673C54F29105A0647204C416A2A:7C3A0D14" ,
46+ TokenMsgs : "QVBBOTFiRzlhcVd2bW54bllCWldHOWh4dnRrZ3pUWFNvcGZpdWZ6bWM2dFAzS2J" ,
47+ AppIDMsgs : "com.cloudsoftphone.app" ,
48+ TokenCalls : "Udl99X2JFP1bWwS5gR/wGeLE1hmAB2CMpr1Ej0wxkrY=" ,
49+ AppIDCalls : "com.cloudsoftphone.app.pushkit" ,
50+ }
51+
52+ body , _ := json .Marshal (reqBody )
53+ req := httptest .NewRequest (http .MethodPost , "/api/client/push_token_report" , bytes .NewBuffer (body ))
54+ req .Header .Set ("Content-Type" , "application/json" )
55+ rec := httptest .NewRecorder ()
56+
57+ c := e .NewContext (req , rec )
58+
59+ h := handler {svc : svc , adminToken : "test" }
60+ err := h .pushTokenReport (c )
61+
62+ // Since we don't have a real database, this will fail with "database not initialized"
63+ // but we can verify the handler processes the request correctly
64+ assert .Error (t , err )
65+ })
66+
67+ t .Run ("invalid json" , func (t * testing.T ) {
68+ req := httptest .NewRequest (http .MethodPost , "/api/client/push_token_report" , bytes .NewBufferString ("invalid json" ))
69+ req .Header .Set ("Content-Type" , "application/json" )
70+ rec := httptest .NewRecorder ()
71+
72+ c := e .NewContext (req , rec )
73+
74+ h := handler {svc : svc , adminToken : "test" }
75+ err := h .pushTokenReport (c )
76+
77+ // Should return a bind error
78+ assert .Error (t , err )
79+ })
80+
81+ t .Run ("empty selector" , func (t * testing.T ) {
82+ reqBody := models.PushTokenReportRequest {
83+ Selector : "" ,
84+ }
85+
86+ body , _ := json .Marshal (reqBody )
87+ req := httptest .NewRequest (http .MethodPost , "/api/client/push_token_report" , bytes .NewBuffer (body ))
88+ req .Header .Set ("Content-Type" , "application/json" )
89+ rec := httptest .NewRecorder ()
90+
91+ c := e .NewContext (req , rec )
92+
93+ h := handler {svc : svc , adminToken : "test" }
94+ err := h .pushTokenReport (c )
95+
96+ assert .Error (t , err )
97+ })
98+ }
0 commit comments