| 1 | package slack |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "net/http" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | var ( |
| 11 | ErrIncorrectResponse = errors.New("Response is incorrect") |
| 12 | ) |
| 13 | |
| 14 | func getTeamInfo(rw http.ResponseWriter, r *http.Request) { |
| 15 | rw.Header().Set("Content-Type", "application/json") |
| 16 | response := []byte(`{"ok": true, "team": { |
| 17 | "id": "F0UWHUX", |
| 18 | "name": "notalar", |
| 19 | "domain": "notalar", |
| 20 | "icon": { |
| 21 | "image_34": "https://slack.global.ssl.fastly.net/66f9/img/avatars-teams/ava_0002-34.png", |
| 22 | "image_44": "https://slack.global.ssl.fastly.net/66f9/img/avatars-teams/ava_0002-44.png", |
| 23 | "image_55": "https://slack.global.ssl.fastly.net/66f9/img/avatars-teams/ava_0002-55.png", |
| 24 | "image_default": true |
| 25 | } |
| 26 | }}`) |
| 27 | rw.Write(response) |
| 28 | } |
| 29 | |
| 30 | func TestGetTeamInfo(t *testing.T) { |
| 31 | http.HandleFunc("/team.info", getTeamInfo) |
| 32 | |
| 33 | once.Do(startServer) |
| 34 | api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/")) |
| 35 | |
| 36 | teamInfo, err := api.GetTeamInfo() |
| 37 | if err != nil { |
| 38 | t.Errorf("Unexpected error: %s", err) |
| 39 | return |
| 40 | } |
| 41 | |
| 42 | // t.Fatal refers to -> t.Errorf & return |
| 43 | if teamInfo.ID != "F0UWHUX" { |
| 44 | t.Fatal(ErrIncorrectResponse) |
| 45 | } |
| 46 | if teamInfo.Domain != "notalar" { |
| 47 | t.Fatal(ErrIncorrectResponse) |
| 48 | } |
| 49 | if teamInfo.Name != "notalar" { |
| 50 | t.Fatal(ErrIncorrectResponse) |
| 51 | } |
| 52 | if teamInfo.Icon == nil { |
| 53 | t.Fatal(ErrIncorrectResponse) |
| 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 | api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/")) |
| 98 | |
| 99 | logins, paging, err := api.GetAccessLogs(NewAccessLogParameters()) |
| 100 | if err != nil { |
| 101 | t.Errorf("Unexpected error: %s", err) |
| 102 | return |
| 103 | } |
| 104 | |
| 105 | if len(logins) != 2 { |
| 106 | t.Fatal("Should have been 2 logins") |
| 107 | } |
| 108 | |
| 109 | // test the first login |
| 110 | login1 := logins[0] |
| 111 | login2 := logins[1] |
| 112 | |
| 113 | if login1.UserID != "F0UWHUX" { |
| 114 | t.Fatal(ErrIncorrectResponse) |
| 115 | } |
| 116 | if login1.Username != "notalar" { |
| 117 | t.Fatal(ErrIncorrectResponse) |
| 118 | } |
| 119 | if login1.DateFirst != 1475684477 { |
| 120 | t.Fatal(ErrIncorrectResponse) |
| 121 | } |
| 122 | if login1.DateLast != 1475684645 { |
| 123 | t.Fatal(ErrIncorrectResponse) |
| 124 | } |
| 125 | if login1.Count != 8 { |
| 126 | t.Fatal(ErrIncorrectResponse) |
| 127 | } |
| 128 | if login1.IP != "127.0.0.1" { |
| 129 | t.Fatal(ErrIncorrectResponse) |
| 130 | } |
| 131 | if !strings.HasPrefix(login1.UserAgent, "SlackWeb") { |
| 132 | t.Fatal(ErrIncorrectResponse) |
| 133 | } |
| 134 | if login1.ISP != "AT&T U-verse" { |
| 135 | t.Fatal(ErrIncorrectResponse) |
| 136 | } |
| 137 | if login1.Country != "US" { |
| 138 | t.Fatal(ErrIncorrectResponse) |
| 139 | } |
| 140 | if login1.Region != "IN" { |
| 141 | t.Fatal(ErrIncorrectResponse) |
| 142 | } |
| 143 | |
| 144 | // test that the null values from login2 are coming across correctly |
| 145 | if login2.ISP != "" { |
| 146 | t.Fatal(ErrIncorrectResponse) |
| 147 | } |
| 148 | if login2.Country != "" { |
| 149 | t.Fatal(ErrIncorrectResponse) |
| 150 | } |
| 151 | if login2.Region != "" { |
| 152 | t.Fatal(ErrIncorrectResponse) |
| 153 | } |
| 154 | |
| 155 | // test the paging |
| 156 | if paging.Count != 2 { |
| 157 | t.Fatal(ErrIncorrectResponse) |
| 158 | } |
| 159 | if paging.Total != 2 { |
| 160 | t.Fatal(ErrIncorrectResponse) |
| 161 | } |
| 162 | if paging.Page != 1 { |
| 163 | t.Fatal(ErrIncorrectResponse) |
| 164 | } |
| 165 | if paging.Pages != 1 { |
| 166 | t.Fatal(ErrIncorrectResponse) |
| 167 | } |
| 168 | } |