Codebase list golang-github-nlopes-slack / b1fe6fb
Finished implementation of Team Access Logs api * Write Tests * Cleaned up some naming Eric Westfall 7 years ago
2 changed file(s) with 129 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
4343 Region string `json:"region"`
4444 }
4545
46 // GetAccessLogsParameters contains all the parameters necessary (including the optional ones) for a GetAccessLogs() request
47 type GetAccessLogsParameters struct {
46 // AccessLogParameters contains all the parameters necessary (including the optional ones) for a GetAccessLogs() request
47 type AccessLogParameters struct {
4848 Count int
4949 Page int
5050 }
51
52 // NewHistoryParameters provides an instance of HistoryParameters with all the sane default values set
53 func NewAccessLogParameters() AccessLogParameters {
54 return AccessLogParameters{
55 Count: DEFAULT_LOGINS_COUNT,
56 Page: DEFAULT_LOGINS_PAGE,
57 }
58 }
59
5160
5261 func teamRequest(path string, values url.Values, debug bool) (*TeamResponse, error) {
5362 response := &TeamResponse{}
6473 }
6574
6675 func accessLogsRequest(path string, values url.Values, debug bool) (*LoginResponse, error) {
67 response := &LoginResponseFull{}
76 response := &LoginResponse{}
6877 err := post(path, values, response, debug)
6978 if err != nil {
7079 return nil, err
9099 }
91100
92101 // GetAccessLogs retrieves a page of logins according to the parameters given
93 func (api *Client) GetAccessLogs(params GetAccessLogsParameters) ([]Login, *Paging, error) {
102 func (api *Client) GetAccessLogs(params AccessLogParameters) ([]Login, *Paging, error) {
94103 values := url.Values{
95104 "token": {api.config.token},
96105 }
33 "errors"
44 "net/http"
55 "testing"
6 "strings"
67 )
78
89 var (
5253 t.Fatal(ErrIncorrectResponse)
5354 }
5455 }
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