Skip to content

Commit 27ffea2

Browse files
committed
fix
1 parent 7552f67 commit 27ffea2

File tree

1 file changed

+111
-271
lines changed

1 file changed

+111
-271
lines changed

main_test.go

Lines changed: 111 additions & 271 deletions
Original file line numberDiff line numberDiff line change
@@ -276,277 +276,6 @@ func getLocalpart(username string) string {
276276
return username
277277
}
278278

279-
func TestIntegration_SendAndFetchMessages(t *testing.T) {
280-
cfg := checkTestEnv(t)
281-
282-
// This test runs against a live homeserver defined in test.env
283-
// It requires the homeserver to be configured with the Application Service.
284-
if os.Getenv("RUN_INTEGRATION_TESTS") == "" {
285-
t.Skip("Skipping integration tests; set RUN_INTEGRATION_TESTS=1 to run.")
286-
}
287-
288-
server, err := startTestServer(cfg)
289-
if err != nil {
290-
t.Fatalf("failed to start test server: %v", err)
291-
}
292-
defer stopTestServer(server)
293-
294-
baseURL := "http://127.0.0.1:" + testServerPort
295-
296-
// Step 1: Send message from USER1 to USER2
297-
t.Run("SendMessage", func(t *testing.T) {
298-
sendReq := models.SendMessageRequest{
299-
From: cfg.user1,
300-
To: cfg.user2,
301-
Body: fmt.Sprintf("Hello from integration test %d", time.Now().Unix()),
302-
}
303-
304-
// First attempt: no password set - must fail
305-
t.Logf("Sending message from %s to %s without password", cfg.user1, cfg.user2)
306-
resp, body, err := doRequest("POST", baseURL+"/api/client/send_message", sendReq, nil)
307-
t.Logf("Initial send_message response: %d: %s", resp.StatusCode, string(body))
308-
if err != nil {
309-
t.Fatalf("request failed: %v", err)
310-
}
311-
if resp.StatusCode == http.StatusOK {
312-
t.Fatalf("expected initial send to fail due to missing password, got 200")
313-
}
314-
// Accept 401 Unauthorized (missing credentials) or 400 Bad Request as failure modes,
315-
// but ensure the initial request did indeed fail because no password was provided.
316-
if resp.StatusCode != http.StatusUnauthorized && resp.StatusCode != http.StatusBadRequest {
317-
t.Fatalf("expected initial failure due to missing password, got %d: %s", resp.StatusCode, string(body))
318-
}
319-
t.Logf("Initial send failed as expected due to missing password: %d: %s", resp.StatusCode, string(body))
320-
321-
// Now set the correct password and retry the send
322-
// (the request struct is expected to include a Password field)
323-
sendReq.Password = cfg.user1Password
324-
325-
resp, body, err = doRequest("POST", baseURL+"/api/client/send_message", sendReq, nil)
326-
if err != nil {
327-
t.Fatalf("request failed: %v", err)
328-
}
329-
if resp.StatusCode != http.StatusOK {
330-
t.Fatalf("send_message returned non-200 status on retry; got %d: %s", resp.StatusCode, string(body))
331-
}
332-
333-
var sendResp models.SendMessageResponse
334-
if err := json.Unmarshal(body, &sendResp); err != nil {
335-
t.Fatalf("failed to parse response: %v", err)
336-
}
337-
if sendResp.ID == "" {
338-
t.Error("expected non-empty message_id")
339-
}
340-
t.Logf("Message sent successfully: %s", sendResp.ID)
341-
342-
// Send a second message from user1 to user2Number (phone number)
343-
sendReqToNumber := models.SendMessageRequest{
344-
From: cfg.user1,
345-
To: cfg.user2Number,
346-
Body: fmt.Sprintf("Hello to number from integration test %d", time.Now().Unix()),
347-
Password: cfg.user1Password,
348-
}
349-
350-
t.Logf("Sending message from %s to number %s", cfg.user1, cfg.user2Number)
351-
resp, body, err = doRequest("POST", baseURL+"/api/client/send_message", sendReqToNumber, nil)
352-
if err != nil {
353-
t.Fatalf("request failed: %v", err)
354-
}
355-
if resp.StatusCode != http.StatusOK {
356-
t.Fatalf("send_message to number returned non-200 status; got %d: %s", resp.StatusCode, string(body))
357-
}
358-
359-
var sendRespNumber models.SendMessageResponse
360-
if err := json.Unmarshal(body, &sendRespNumber); err != nil {
361-
t.Fatalf("failed to parse response: %v", err)
362-
}
363-
if sendRespNumber.ID == "" {
364-
t.Error("expected non-empty message_id for number send")
365-
}
366-
t.Logf("Message to number sent successfully: %s", sendRespNumber.ID)
367-
})
368-
369-
// Step 2: Fetch messages as USER2 to confirm receipt
370-
t.Run("FetchMessages", func(t *testing.T) {
371-
fetchResp, err := fetchMessagesWithRetry(t, baseURL, cfg.user2, 10*time.Second)
372-
if err != nil {
373-
t.Fatalf("fetch messages failed: %v", err)
374-
}
375-
376-
found := false
377-
for _, msg := range fetchResp.ReceivedSMSs {
378-
if strings.Contains(msg.SMSText, "Hello from integration test") && msg.Sender == cfg.user1Number {
379-
found = true
380-
t.Logf("Found test message from %s", msg.Sender)
381-
break
382-
}
383-
}
384-
if !found {
385-
t.Errorf("test message not found in received messages for %s", cfg.user2)
386-
}
387-
})
388-
}
389-
390-
func TestIntegration_RoomMessaging(t *testing.T) {
391-
cfg := checkTestEnv(t)
392-
393-
// This test runs against a live homeserver defined in test.env
394-
// It requires the homeserver to be configured with the Application Service.
395-
if os.Getenv("RUN_INTEGRATION_TESTS") == "" {
396-
t.Skip("Skipping integration tests; set RUN_INTEGRATION_TESTS=1 to run.")
397-
}
398-
399-
server, err := startTestServer(cfg)
400-
if err != nil {
401-
t.Fatalf("failed to start test server: %v", err)
402-
}
403-
defer stopTestServer(server)
404-
405-
baseURL := "http://127.0.0.1:" + testServerPort
406-
user1Localpart := getLocalpart(cfg.user1)
407-
user2Localpart := getLocalpart(cfg.user2)
408-
user1MatrixID := fmt.Sprintf("@%s:%s", user1Localpart, cfg.serverName)
409-
user2MatrixID := fmt.Sprintf("@%s:%s", user2Localpart, cfg.serverName)
410-
411-
// Create a room and have both users join
412-
t.Run("CreateRoomAndExchangeMessages", func(t *testing.T) {
413-
// Step 1: Create a room as user1
414-
matrixClient, err := matrix.NewClient(matrix.Config{
415-
HomeserverURL: cfg.homeserverURL,
416-
AsUserID: id.UserID(cfg.asUser),
417-
AsToken: cfg.adminToken,
418-
})
419-
if err != nil {
420-
t.Fatalf("failed to create matrix client: %v", err)
421-
}
422-
423-
// Create a direct room between user1 and user2 using CreateDirectRoom
424-
aliasKey := fmt.Sprintf("test_room_%d", time.Now().Unix())
425-
createResp, err := matrixClient.CreateDirectRoom(context.Background(), id.UserID(user1MatrixID), id.UserID(user2MatrixID), aliasKey)
426-
if err != nil {
427-
t.Fatalf("failed to create direct room: %v", err)
428-
}
429-
roomID := createResp.RoomID
430-
t.Logf("Created direct room %s", roomID)
431-
432-
// Step 2: Join the room as user2
433-
_, err = matrixClient.JoinRoom(context.Background(), id.UserID(user2MatrixID), roomID)
434-
if err != nil {
435-
t.Fatalf("failed for user2 to join room: %v", err)
436-
}
437-
t.Logf("User2 joined room %s", roomID)
438-
439-
// Give the server time to process the join event
440-
time.Sleep(5000 * time.Millisecond)
441-
442-
// Initial fetch for both users to establish baseline sync tokens
443-
_, _ = fetchMessagesWithRetry(t, baseURL, user1MatrixID, 2*time.Second)
444-
_, _ = fetchMessagesWithRetry(t, baseURL, user2MatrixID, 2*time.Second)
445-
446-
// Step 3: User1 sends a message to the room
447-
sendReq1 := models.SendMessageRequest{
448-
From: user1MatrixID,
449-
To: string(roomID), // Send to room ID
450-
Body: fmt.Sprintf("Hello from user1 %d", time.Now().Unix()),
451-
}
452-
resp, body, err := doRequest("POST", baseURL+"/api/client/send_message", sendReq1, nil)
453-
if err != nil {
454-
t.Fatalf("request failed: %v", err)
455-
}
456-
if resp.StatusCode != http.StatusOK {
457-
t.Logf("send_message returned non-200 status; got %d: %s", resp.StatusCode, string(body))
458-
if resp.StatusCode == http.StatusBadRequest {
459-
t.Skip("recipient not resolvable in this environment; skipping assertion")
460-
}
461-
t.Fatalf("unexpected status code %d: %s", resp.StatusCode, string(body))
462-
}
463-
var sendResp1 models.SendMessageResponse
464-
if err := json.Unmarshal(body, &sendResp1); err != nil {
465-
t.Fatalf("failed to parse response: %v", err)
466-
}
467-
t.Logf("User1 message sent: %s", sendResp1.ID)
468-
469-
// Wait for message propagation
470-
time.Sleep(1 * time.Second)
471-
472-
// Step 4: User2 sends a message to the room
473-
sendReq2 := models.SendMessageRequest{
474-
From: user2MatrixID,
475-
To: string(roomID), // Send to room ID
476-
Body: fmt.Sprintf("Hello from user2 %d", time.Now().Unix()),
477-
}
478-
resp, body, err = doRequest("POST", baseURL+"/api/client/send_message", sendReq2, nil)
479-
if err != nil {
480-
t.Fatalf("request failed: %v", err)
481-
}
482-
if resp.StatusCode != http.StatusOK {
483-
t.Fatalf("expected 200, got %d: %s", resp.StatusCode, string(body))
484-
}
485-
var sendResp2 models.SendMessageResponse
486-
if err := json.Unmarshal(body, &sendResp2); err != nil {
487-
t.Fatalf("failed to parse response: %v", err)
488-
}
489-
t.Logf("User2 message sent: %s", sendResp2.ID)
490-
491-
// Wait for message propagation
492-
time.Sleep(1 * time.Second)
493-
494-
// Step 5: User1 fetches messages and should see user2's message
495-
fetchResp1, err := fetchMessagesWithRetry(t, baseURL, user1MatrixID, 10*time.Second)
496-
if err != nil {
497-
t.Fatalf("fetch messages failed: %v", err)
498-
}
499-
500-
// Check that user1 sees the message from user2
501-
foundUser2Message := false
502-
for _, msg := range fetchResp1.ReceivedSMSs {
503-
if strings.Contains(msg.SMSText, "Hello from user2") {
504-
foundUser2Message = true
505-
t.Logf("User1 received message from user2: %s", msg.SMSText)
506-
break
507-
}
508-
}
509-
if !foundUser2Message {
510-
t.Errorf("user1 did not receive message from user2")
511-
}
512-
513-
// Step 6: User2 fetches messages and should see user1's message
514-
// Do a fresh fetch to ensure we get all messages
515-
fetchResp2, err := fetchMessagesWithRetry(t, baseURL, user2MatrixID, 10*time.Second)
516-
if err != nil {
517-
t.Fatalf("fetch messages failed: %v", err)
518-
}
519-
520-
// Check that user2 sees the message from user1 in received or sent messages
521-
foundUser1Message := false
522-
for _, msg := range fetchResp2.ReceivedSMSs {
523-
if strings.Contains(msg.SMSText, "Hello from user1") {
524-
foundUser1Message = true
525-
t.Logf("User2 received message from user1: %s", msg.SMSText)
526-
break
527-
}
528-
}
529-
if !foundUser1Message {
530-
t.Logf("Message from user1 not found in received messages, checking all messages. Received: %v, Sent: %v", len(fetchResp2.ReceivedSMSs), len(fetchResp2.SentSMSs))
531-
t.Errorf("user2 did not receive message from user1")
532-
}
533-
534-
// Check that user2 sees their own message in sent messages
535-
foundOwnMessage := false
536-
for _, msg := range fetchResp2.SentSMSs {
537-
if strings.Contains(msg.SMSText, "Hello from user2") {
538-
foundOwnMessage = true
539-
t.Logf("User2 sees their own sent message: %s", msg.SMSText)
540-
break
541-
}
542-
}
543-
if !foundOwnMessage {
544-
t.Logf("Own message not found in sent messages. Sent count: %d", len(fetchResp2.SentSMSs))
545-
t.Errorf("user2 did not see their own sent message")
546-
}
547-
})
548-
}
549-
550279
func TestIntegration_PushTokenReport(t *testing.T) {
551280
cfg := checkTestEnv(t)
552281

@@ -684,3 +413,114 @@ func TestIntegration_PushTokenReport(t *testing.T) {
684413
t.Logf("Push token reported successfully for %s with selector: %s", cfg.user2, selector)
685414
})
686415
}
416+
417+
func TestIntegration_SendAndFetchMessages(t *testing.T) {
418+
cfg := checkTestEnv(t)
419+
420+
// This test runs against a live homeserver defined in test.env
421+
// It requires the homeserver to be configured with the Application Service.
422+
if os.Getenv("RUN_INTEGRATION_TESTS") == "" {
423+
t.Skip("Skipping integration tests; set RUN_INTEGRATION_TESTS=1 to run.")
424+
}
425+
426+
server, err := startTestServer(cfg)
427+
if err != nil {
428+
t.Fatalf("failed to start test server: %v", err)
429+
}
430+
defer stopTestServer(server)
431+
432+
baseURL := "http://127.0.0.1:" + testServerPort
433+
434+
// Step 1: Send message from USER1 to USER2
435+
t.Run("SendMessage", func(t *testing.T) {
436+
sendReq := models.SendMessageRequest{
437+
From: cfg.user1,
438+
To: cfg.user2,
439+
Body: fmt.Sprintf("Hello from integration test %d", time.Now().Unix()),
440+
}
441+
442+
// First attempt: no password set - must fail
443+
t.Logf("Sending message from %s to %s without password", cfg.user1, cfg.user2)
444+
resp, body, err := doRequest("POST", baseURL+"/api/client/send_message", sendReq, nil)
445+
t.Logf("Initial send_message response: %d: %s", resp.StatusCode, string(body))
446+
if err != nil {
447+
t.Fatalf("request failed: %v", err)
448+
}
449+
if resp.StatusCode == http.StatusOK {
450+
t.Fatalf("expected initial send to fail due to missing password, got 200")
451+
}
452+
// Accept 401 Unauthorized (missing credentials) or 400 Bad Request as failure modes,
453+
// but ensure the initial request did indeed fail because no password was provided.
454+
if resp.StatusCode != http.StatusUnauthorized && resp.StatusCode != http.StatusBadRequest {
455+
t.Fatalf("expected initial failure due to missing password, got %d: %s", resp.StatusCode, string(body))
456+
}
457+
t.Logf("Initial send failed as expected due to missing password: %d: %s", resp.StatusCode, string(body))
458+
459+
// Now set the correct password and retry the send
460+
// (the request struct is expected to include a Password field)
461+
sendReq.Password = cfg.user1Password
462+
463+
resp, body, err = doRequest("POST", baseURL+"/api/client/send_message", sendReq, nil)
464+
if err != nil {
465+
t.Fatalf("request failed: %v", err)
466+
}
467+
if resp.StatusCode != http.StatusOK {
468+
t.Fatalf("send_message returned non-200 status on retry; got %d: %s", resp.StatusCode, string(body))
469+
}
470+
471+
var sendResp models.SendMessageResponse
472+
if err := json.Unmarshal(body, &sendResp); err != nil {
473+
t.Fatalf("failed to parse response: %v", err)
474+
}
475+
if sendResp.ID == "" {
476+
t.Error("expected non-empty message_id")
477+
}
478+
t.Logf("Message sent successfully: %s", sendResp.ID)
479+
480+
// Send a second message from user1 to user2Number (phone number)
481+
sendReqToNumber := models.SendMessageRequest{
482+
From: cfg.user1,
483+
To: cfg.user2Number,
484+
Body: fmt.Sprintf("Hello to number from integration test %d", time.Now().Unix()),
485+
Password: cfg.user1Password,
486+
}
487+
488+
t.Logf("Sending message from %s to number %s", cfg.user1, cfg.user2Number)
489+
resp, body, err = doRequest("POST", baseURL+"/api/client/send_message", sendReqToNumber, nil)
490+
if err != nil {
491+
t.Fatalf("request failed: %v", err)
492+
}
493+
if resp.StatusCode != http.StatusOK {
494+
t.Fatalf("send_message to number returned non-200 status; got %d: %s", resp.StatusCode, string(body))
495+
}
496+
497+
var sendRespNumber models.SendMessageResponse
498+
if err := json.Unmarshal(body, &sendRespNumber); err != nil {
499+
t.Fatalf("failed to parse response: %v", err)
500+
}
501+
if sendRespNumber.ID == "" {
502+
t.Error("expected non-empty message_id for number send")
503+
}
504+
t.Logf("Message to number sent successfully: %s", sendRespNumber.ID)
505+
})
506+
507+
// Step 2: Fetch messages as USER2 to confirm receipt
508+
t.Run("FetchMessages", func(t *testing.T) {
509+
fetchResp, err := fetchMessagesWithRetry(t, baseURL, cfg.user2, 10*time.Second)
510+
if err != nil {
511+
t.Fatalf("fetch messages failed: %v", err)
512+
}
513+
514+
found := false
515+
for _, msg := range fetchResp.ReceivedSMSs {
516+
if strings.Contains(msg.SMSText, "Hello from integration test") && msg.Sender == cfg.user1Number {
517+
found = true
518+
t.Logf("Found test message from %s", msg.Sender)
519+
break
520+
}
521+
}
522+
if !found {
523+
t.Errorf("test message not found in received messages for %s", cfg.user2)
524+
}
525+
})
526+
}

0 commit comments

Comments
 (0)