| 1 | package slack |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func getAuditLogs(rw http.ResponseWriter, r *http.Request) { |
| 9 | rw.Header().Set("Content-Type", "application/json") |
| 10 | response := []byte(`{"entries": [ |
| 11 | { |
| 12 | "id": "0123a45b-6c7d-8900-e12f-3456789gh0i1", |
| 13 | "date_create": 1521214343, |
| 14 | "action": "user_login", |
| 15 | "actor": { |
| 16 | "type": "user", |
| 17 | "user": { |
| 18 | "id": "W123AB456", |
| 19 | "name": "Charlie Parker", |
| 20 | "email": "[email protected]" |
| 21 | } |
| 22 | }, |
| 23 | "entity": { |
| 24 | "type": "user", |
| 25 | "user": { |
| 26 | "id": "W123AB456", |
| 27 | "name": "Charlie Parker", |
| 28 | "email": "[email protected]" |
| 29 | } |
| 30 | }, |
| 31 | "context": { |
| 32 | "location": { |
| 33 | "type": "enterprise", |
| 34 | "id": "E1701NCCA", |
| 35 | "name": "Birdland", |
| 36 | "domain": "birdland" |
| 37 | }, |
| 38 | "ua": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36", |
| 39 | "ip_address": "1.23.45.678" |
| 40 | } |
| 41 | } |
| 42 | ] |
| 43 | }`) |
| 44 | rw.Write(response) |
| 45 | } |
| 46 | |
| 47 | func TestGetAuditLogs(t *testing.T) { |
| 48 | http.HandleFunc("/audit/v1/logs", getAuditLogs) |
| 49 | |
| 50 | once.Do(startServer) |
| 51 | api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/")) |
| 52 | |
| 53 | events, nextCursor, err := api.GetAuditLogs(AuditLogParameters{}) |
| 54 | if err != nil { |
| 55 | t.Errorf("Unexpected error: %s", err) |
| 56 | return |
| 57 | } |
| 58 | |
| 59 | if len(events) != 1 { |
| 60 | t.Fatal("Should have been 1 event") |
| 61 | } |
| 62 | |
| 63 | // test the first login |
| 64 | event1 := events[0] |
| 65 | |
| 66 | if event1.Action != "user_login" { |
| 67 | t.Fatal(ErrIncorrectResponse) |
| 68 | } |
| 69 | if event1.Entity.User.Email != "[email protected]" { |
| 70 | t.Fatal(ErrIncorrectResponse) |
| 71 | } |
| 72 | if event1.Context.Location.Domain != "birdland" { |
| 73 | t.Fatal(ErrIncorrectResponse) |
| 74 | } |
| 75 | if event1.DateCreate != 1521214343 { |
| 76 | t.Fatal(ErrIncorrectResponse) |
| 77 | } |
| 78 | if event1.Context.IPAddress != "1.23.45.678" { |
| 79 | t.Fatal(ErrIncorrectResponse) |
| 80 | } |
| 81 | |
| 82 | if nextCursor != "" { |
| 83 | t.Fatal(ErrIncorrectResponse) |
| 84 | } |
| 85 | } |