MM-54182 - add correct information to session table from mobile devices (#24353)

* MM-54182 - add correct information to session table from mobile devices

* improve comments around helper function

---------

Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
Pablo Andrés Vélez Vidal
2023-09-01 14:25:07 +02:00
коммит произвёл GitHub
родитель 8c2fc88471
Коммит 4f0f3845e4
5 изменённых файлов: 67 добавлений и 4 удалений

Просмотреть файл

@@ -208,6 +208,25 @@ func IsValidMobileAuthRedirectURL(config *model.Config, redirectURL string) bool
return false
}
// This will only return TRUE if the request comes from a mobile running the Mobile App.
// If the request comes from a mobile using the browser, it will return FALSE.
func IsMobileRequest(r *http.Request) bool {
userAgent := r.UserAgent()
if userAgent == "" {
return false
}
// Check if the User-Agent contain keywords found in mobile devices running the mobile App
mobileKeywords := []string{"Mobile", "Android", "iOS", "iPhone", "iPad"}
for _, keyword := range mobileKeywords {
if strings.Contains(userAgent, keyword) {
return true
}
}
return false
}
// RoundOffToZeroes converts all digits to 0 except the 1st one.
// Special case: If there is only 1 digit, then returns 0.
func RoundOffToZeroes(n float64) int64 {

Просмотреть файл

@@ -5,6 +5,7 @@ package utils
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
@@ -279,6 +280,32 @@ func TestRoundOffToZeroes(t *testing.T) {
}
}
func TestIsMobileRequest(t *testing.T) {
testCases := []struct {
userAgent string
expected bool
}{
// Test cases with mobile devices
{"Mozilla/5.0 (iPhone; CPU iPhone OS 15_0 like Mac OS X)", true},
{"Mozilla/5.0 (Android 12; Mobile)", true},
{"Mozilla/5.0 (Linux; Android 12; Pixel 6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.9999.99 Mobile Safari/537.36", true},
// Test cases with NO mobile devices
{"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36", false},
{"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36", false},
}
for _, tc := range testCases {
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.Header.Set("User-Agent", tc.userAgent)
result := IsMobileRequest(req)
if result != tc.expected {
t.Errorf("User-Agent: %s, expected: %v, got: %v", tc.userAgent, tc.expected, result)
}
}
}
func TestRoundOffToZeroesResolution(t *testing.T) {
messageGranularity := 3
storageGranularity := 8