github.com/haalcala/mattermost-server-change-repo/v5@v5.33.2/app/user_agent_test.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package app
     5  
     6  import (
     7  	"fmt"
     8  	"testing"
     9  
    10  	"github.com/avct/uasurfer"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  type testUserAgent struct {
    15  	Name      string
    16  	UserAgent string
    17  }
    18  
    19  var testUserAgents = []testUserAgent{
    20  	{"Mozilla 40.1", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1"},
    21  	{"Chrome 60", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36"},
    22  	{"Chrome Mobile", "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Mobile Safari/537.36"},
    23  	{"MM Classic App", "Mozilla/5.0 (Linux; Android 8.0.0; Nexus 5X Build/OPR6.170623.013; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/61.0.3163.81 Mobile Safari/537.36 Web-Atoms-Mobile-WebView"},
    24  	{"mmctl", "mmctl/5.20.0 (linux)"},
    25  	{"MM App 3.7.1", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Mattermost/3.7.1 Chrome/56.0.2924.87 Electron/1.6.11 Safari/537.36"},
    26  	{"Franz 4.0.4", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Franz/4.0.4 Chrome/52.0.2743.82 Electron/1.3.1 Safari/537.36"},
    27  	{"Edge 14", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393"},
    28  	{"Internet Explorer 9", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 7.1; Trident/5.0"},
    29  	{"Internet Explorer 11", "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko"},
    30  	{"Internet Explorer 11 2", "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; Zoom 3.6.0; rv:11.0) like Gecko"},
    31  	{"Internet Explorer 11 (Compatibility Mode) 1", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; .NET CLR 1.1.4322; InfoPath.3; Zoom 3.6.0)"},
    32  	{"Internet Explorer 11 (Compatibility Mode) 2", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; Zoom 3.6.0)"},
    33  	{"Safari 9", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Safari/604.1.38"},
    34  	{"Safari 8", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/600.7.12 (KHTML, like Gecko) Version/8.0.7 Safari/600.7.12"},
    35  	{"Safari Mobile", "Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B137 Safari/601.1"},
    36  }
    37  
    38  func TestGetPlatformName(t *testing.T) {
    39  	expected := []string{
    40  		"Windows",
    41  		"Macintosh",
    42  		"Linux",
    43  		"Linux",
    44  		"Linux",
    45  		"Macintosh",
    46  		"Macintosh",
    47  		"Windows",
    48  		"Windows",
    49  		"Windows",
    50  		"Windows",
    51  		"Windows",
    52  		"Windows",
    53  		"Macintosh",
    54  		"Macintosh",
    55  		"iPhone",
    56  	}
    57  
    58  	for i, userAgent := range testUserAgents {
    59  		t.Run(fmt.Sprintf("GetPlatformName_%v", i), func(t *testing.T) {
    60  			ua := uasurfer.Parse(userAgent.UserAgent)
    61  
    62  			actual := getPlatformName(ua)
    63  			assert.Equal(t, expected[i], actual)
    64  		})
    65  	}
    66  }
    67  
    68  func TestGetOSName(t *testing.T) {
    69  	expected := []string{
    70  		"Windows 7",
    71  		"Mac OS",
    72  		"Android",
    73  		"Android",
    74  		"Linux",
    75  		"Mac OS",
    76  		"Mac OS",
    77  		"Windows 10",
    78  		"Windows",
    79  		"Windows 10",
    80  		"Windows 10",
    81  		"Windows 10",
    82  		"Windows 10",
    83  		"Mac OS",
    84  		"Mac OS",
    85  		"iOS",
    86  	}
    87  
    88  	for i, userAgent := range testUserAgents {
    89  		t.Run(fmt.Sprintf("GetOSName_%v", i), func(t *testing.T) {
    90  			ua := uasurfer.Parse(userAgent.UserAgent)
    91  
    92  			actual := getOSName(ua)
    93  			assert.Equal(t, expected[i], actual)
    94  		})
    95  	}
    96  }
    97  
    98  func TestGetBrowserName(t *testing.T) {
    99  	expected := []string{
   100  		"Firefox",
   101  		"Chrome",
   102  		"Chrome",
   103  		"Chrome",
   104  		"mmctl",
   105  		"Desktop App",
   106  		"Chrome",
   107  		"Edge",
   108  		"Internet Explorer",
   109  		"Internet Explorer",
   110  		"Internet Explorer",
   111  		"Internet Explorer",
   112  		"Internet Explorer",
   113  		"Safari",
   114  		"Safari",
   115  		"Safari",
   116  	}
   117  
   118  	for i, userAgent := range testUserAgents {
   119  		t.Run(fmt.Sprintf("GetBrowserName_%v", i), func(t *testing.T) {
   120  			ua := uasurfer.Parse(userAgent.UserAgent)
   121  
   122  			actual := getBrowserName(ua, userAgent.UserAgent)
   123  			assert.Equal(t, expected[i], actual)
   124  		})
   125  	}
   126  }
   127  
   128  func TestGetBrowserVersion(t *testing.T) {
   129  	expected := []string{
   130  		"40.1",
   131  		"60.0.3112", // Doesn't report the fourth part of the version
   132  		"60.0.3112", // Doesn't report the fourth part of the version
   133  		"61.0.3163",
   134  		"5.20.0",
   135  		"3.7.1",
   136  		"4.0.4",
   137  		"14.14393",
   138  		"9.0",
   139  		"11.0",
   140  		"11.0",
   141  		"7.0",
   142  		"7.0",
   143  		"11.0",
   144  		"8.0.7",
   145  		"9.0",
   146  	}
   147  
   148  	for i, userAgent := range testUserAgents {
   149  		t.Run(fmt.Sprintf("GetBrowserVersion_%v", i), func(t *testing.T) {
   150  			ua := uasurfer.Parse(userAgent.UserAgent)
   151  
   152  			actual := getBrowserVersion(ua, userAgent.UserAgent)
   153  			assert.Equal(t, expected[i], actual)
   154  		})
   155  	}
   156  }