3 | 3 |
"errors"
|
4 | 4 |
"net/http"
|
5 | 5 |
"testing"
|
|
6 |
"strings"
|
6 | 7 |
)
|
7 | 8 |
|
8 | 9 |
var (
|
|
52 | 53 |
t.Fatal(ErrIncorrectResponse)
|
53 | 54 |
}
|
54 | 55 |
}
|
|
56 |
|
|
57 |
func getTeamAccessLogs(rw http.ResponseWriter, r *http.Request) {
|
|
58 |
rw.Header().Set("Content-Type", "application/json")
|
|
59 |
response := []byte(`{"ok": true, "logins": [{
|
|
60 |
"user_id": "F0UWHUX",
|
|
61 |
"username": "notalar",
|
|
62 |
"date_first": 1475684477,
|
|
63 |
"date_last": 1475684645,
|
|
64 |
"count": 8,
|
|
65 |
"ip": "127.0.0.1",
|
|
66 |
"user_agent": "SlackWeb/3abb0ae2380d48a9ae20c58cc624ebcd Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Slack/1.2.6 Chrome/45.0.2454.85 AtomShell/0.34.3 Safari/537.36 Slack_SSB/1.2.6",
|
|
67 |
"isp": "AT&T U-verse",
|
|
68 |
"country": "US",
|
|
69 |
"region": "IN"
|
|
70 |
},
|
|
71 |
{
|
|
72 |
"user_id": "XUHWU0F",
|
|
73 |
"username": "ralaton",
|
|
74 |
"date_first": 1447395893,
|
|
75 |
"date_last": 1447395965,
|
|
76 |
"count": 5,
|
|
77 |
"ip": "192.168.0.1",
|
|
78 |
"user_agent": "com.tinyspeck.chatlyio/2.60 (iPhone; iOS 9.1; Scale/3.00)",
|
|
79 |
"isp": null,
|
|
80 |
"country": null,
|
|
81 |
"region": null
|
|
82 |
}],
|
|
83 |
"paging": {
|
|
84 |
"count": 2,
|
|
85 |
"total": 2,
|
|
86 |
"page": 1,
|
|
87 |
"pages": 1
|
|
88 |
}
|
|
89 |
}`)
|
|
90 |
rw.Write(response)
|
|
91 |
}
|
|
92 |
|
|
93 |
func TestGetAccessLogs(t *testing.T) {
|
|
94 |
http.HandleFunc("/team.accessLogs", getTeamAccessLogs)
|
|
95 |
|
|
96 |
once.Do(startServer)
|
|
97 |
SLACK_API = "http://" + serverAddr + "/"
|
|
98 |
api := New("testing-token")
|
|
99 |
|
|
100 |
logins, paging, err := api.GetAccessLogs(NewAccessLogParameters())
|
|
101 |
if err != nil {
|
|
102 |
t.Errorf("Unexpected error: %s", err)
|
|
103 |
return
|
|
104 |
}
|
|
105 |
|
|
106 |
if len(logins) != 2 {
|
|
107 |
t.Fatal("Should have been 2 logins")
|
|
108 |
}
|
|
109 |
|
|
110 |
// test the first login
|
|
111 |
login1 := logins[0]
|
|
112 |
login2 := logins[1]
|
|
113 |
|
|
114 |
if (login1.UserID != "F0UWHUX") {
|
|
115 |
t.Fatal(ErrIncorrectResponse)
|
|
116 |
}
|
|
117 |
if (login1.Username != "notalar") {
|
|
118 |
t.Fatal(ErrIncorrectResponse)
|
|
119 |
}
|
|
120 |
if (login1.DateFirst != 1475684477) {
|
|
121 |
t.Fatal(ErrIncorrectResponse)
|
|
122 |
}
|
|
123 |
if (login1.DateLast != 1475684645) {
|
|
124 |
t.Fatal(ErrIncorrectResponse)
|
|
125 |
}
|
|
126 |
if (login1.Count != 8) {
|
|
127 |
t.Fatal(ErrIncorrectResponse)
|
|
128 |
}
|
|
129 |
if (login1.IP != "127.0.0.1") {
|
|
130 |
t.Fatal(ErrIncorrectResponse)
|
|
131 |
}
|
|
132 |
if (!strings.HasPrefix(login1.UserAgent, "SlackWeb")) {
|
|
133 |
t.Fatal(ErrIncorrectResponse)
|
|
134 |
}
|
|
135 |
if (login1.ISP != "AT&T U-verse") {
|
|
136 |
t.Fatal(ErrIncorrectResponse)
|
|
137 |
}
|
|
138 |
if (login1.Country != "US") {
|
|
139 |
t.Fatal(ErrIncorrectResponse)
|
|
140 |
}
|
|
141 |
if (login1.Region != "IN") {
|
|
142 |
t.Fatal(ErrIncorrectResponse)
|
|
143 |
}
|
|
144 |
|
|
145 |
// test that the null values from login2 are coming across correctly
|
|
146 |
if (login2.ISP != "") {
|
|
147 |
t.Fatal(ErrIncorrectResponse)
|
|
148 |
}
|
|
149 |
if (login2.Country != "") {
|
|
150 |
t.Fatal(ErrIncorrectResponse)
|
|
151 |
}
|
|
152 |
if (login2.Region != "") {
|
|
153 |
t.Fatal(ErrIncorrectResponse)
|
|
154 |
}
|
|
155 |
|
|
156 |
// test the paging
|
|
157 |
if (paging.Count != 2) {
|
|
158 |
t.Fatal(ErrIncorrectResponse)
|
|
159 |
}
|
|
160 |
if (paging.Total != 2) {
|
|
161 |
t.Fatal(ErrIncorrectResponse)
|
|
162 |
}
|
|
163 |
if (paging.Page != 1) {
|
|
164 |
t.Fatal(ErrIncorrectResponse)
|
|
165 |
}
|
|
166 |
if (paging.Pages != 1) {
|
|
167 |
t.Fatal(ErrIncorrectResponse)
|
|
168 |
}
|
|
169 |
}
|
|
170 |
|