forked from matrix-org/dendrite
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
matrix-org#3333 used ts parameter for appservices
The issue involved ensuring that the ts parameter in Dendrite is correctly handled when the request is from an appservice, and that it is ignored or handled differently when not. The resolution was achieved through the following steps: Refactoring the Logic: The logic for processing the ts parameter was refactored into a separate function named HandleEventTimestamp. This function determines whether the request is from an appservice by calling isAppService(req). If it is, the function parses the ts parameter using httputil.ParseTSParam(req). If the request is not from an appservice, the function defaults to using the current time or another appropriate handling. Updating sendevent.go: The inline logic in sendevent.go that handled the ts parameter was replaced with a call to the new HandleEventTimestamp function. This ensures that the logic is centralized and can be easily tested and maintained. Creating Tests: A new test file, ts_param_test.go, was created in the testing directory. This file includes tests that cover various scenarios: When the ts parameter is valid and the request is from an appservice. When the ts parameter is invalid or missing. When the request is not from an appservice. Signed off by : `Srinjoy Sen Chowdhury [email protected]`
- Loading branch information
1 parent
eb6c8af
commit 35611c4
Showing
2 changed files
with
108 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// ts_param_test.go | ||
package testing | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
"your_project_path/routing" // Adjust this import path | ||
) | ||
|
||
func createRequestWithTS(ts string, isAppService bool) *http.Request { | ||
req := httptest.NewRequest("POST", "/_matrix/client/r0/rooms/!roomid:domain/send/m.room.message", nil) | ||
q := req.URL.Query() | ||
if ts != "" { | ||
q.Add("ts", ts) | ||
} | ||
req.URL.RawQuery = q.Encode() | ||
|
||
if isAppService { | ||
req.Header.Set("Authorization", "Bearer your_appservice_token") | ||
} else { | ||
req.Header.Set("Authorization", "Bearer regular_user_token") | ||
} | ||
return req | ||
} | ||
|
||
func TestHandleEventTimestamp_ValidAppService(t *testing.T) { | ||
req := createRequestWithTS("1657890000000", true) | ||
evTime, err := routing.HandleEventTimestamp(req) | ||
if err != nil { | ||
t.Fatalf("Expected no error, got %v", err) | ||
} | ||
|
||
expectedTime := time.Unix(1657890000, 0) | ||
if !evTime.Equal(expectedTime) { | ||
t.Errorf("Expected time %v, got %v", expectedTime, evTime) | ||
} | ||
} | ||
|
||
func TestHandleEventTimestamp_InvalidTS(t *testing.T) { | ||
req := createRequestWithTS("invalid_ts", true) | ||
_, err := routing.HandleEventTimestamp(req) | ||
if err == nil { | ||
t.Fatal("Expected an error, got none") | ||
} | ||
} | ||
|
||
func TestHandleEventTimestamp_NonAppService(t *testing.T) { | ||
req := createRequestWithTS("1657890000000", false) | ||
evTime, err := routing.HandleEventTimestamp(req) | ||
if err != nil { | ||
t.Fatalf("Expected no error, got %v", err) | ||
} | ||
|
||
if time.Now().Sub(evTime) > time.Second { | ||
t.Errorf("Expected current time, got %v", evTime) | ||
} | ||
} |