85 lines
2.4 KiB
Go
85 lines
2.4 KiB
Go
package errorlog
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"chatapp3-golang/internal/common"
|
|
"chatapp3-golang/internal/model"
|
|
|
|
"gorm.io/driver/sqlite"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func TestReportAgoraErrorDetectsBannedByServer(t *testing.T) {
|
|
service := newTestService(t)
|
|
tokenOK := false
|
|
timeout := true
|
|
|
|
resp, err := service.ReportAgoraError(context.Background(), common.AuthUser{
|
|
UserID: 1234567,
|
|
SysOrigin: "LIKEI",
|
|
}, AgoraErrorReportRequest{
|
|
RoomID: StringValue("9001"),
|
|
AgoraUID: StringValue("8888"),
|
|
ErrorCodeType: "ERR_LEAVE_CHANNEL_REJECTED",
|
|
ConnectionChangedReasonType: "BannedByServer(3)",
|
|
Timeout: &timeout,
|
|
TokenRequestSuccess: &tokenOK,
|
|
NetworkType: "wifi",
|
|
}, AgoraErrorReportContext{
|
|
ReqClient: "Android",
|
|
ReqAppIntel: "version=1.0.0;build=100",
|
|
ClientIP: "127.0.0.1",
|
|
RawPayload: `{"roomId":"9001","connectionChangedReasonType":"BannedByServer(3)"}`,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("ReportAgoraError returned error: %v", err)
|
|
}
|
|
if !resp.BannedByServer {
|
|
t.Fatalf("BannedByServer = false, want true")
|
|
}
|
|
|
|
page, err := service.PageAgoraErrorLogs(context.Background(), AgoraErrorLogQuery{
|
|
SysOrigin: "LIKEI",
|
|
BannedOnly: true,
|
|
Cursor: 1,
|
|
Limit: 20,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("PageAgoraErrorLogs returned error: %v", err)
|
|
}
|
|
if page.Total != 1 || len(page.Records) != 1 {
|
|
t.Fatalf("page total=%d records=%d, want 1", page.Total, len(page.Records))
|
|
}
|
|
record := page.Records[0]
|
|
if record.RoomID != "9001" || record.AgoraUID != "8888" || !record.IsTimeout {
|
|
t.Fatalf("unexpected record: %+v", record)
|
|
}
|
|
if record.TokenRequestSuccess == nil || *record.TokenRequestSuccess {
|
|
t.Fatalf("TokenRequestSuccess = %v, want false", record.TokenRequestSuccess)
|
|
}
|
|
}
|
|
|
|
func TestStringValueAcceptsNumericJSON(t *testing.T) {
|
|
var value StringValue
|
|
if err := value.UnmarshalJSON([]byte(`1838887943867809794`)); err != nil {
|
|
t.Fatalf("UnmarshalJSON returned error: %v", err)
|
|
}
|
|
if value.String() != "1838887943867809794" {
|
|
t.Fatalf("value = %q, want numeric string", value.String())
|
|
}
|
|
}
|
|
|
|
func newTestService(t *testing.T) *Service {
|
|
t.Helper()
|
|
db, err := gorm.Open(sqlite.Open("file:"+t.Name()+"?mode=memory&cache=shared"), &gorm.Config{})
|
|
if err != nil {
|
|
t.Fatalf("open sqlite: %v", err)
|
|
}
|
|
if err := db.AutoMigrate(&model.AgoraErrorLog{}); err != nil {
|
|
t.Fatalf("auto migrate: %v", err)
|
|
}
|
|
return NewService(db)
|
|
}
|