github.com/go-chef/chef@v0.30.1/association_test.go (about) 1 package chef 2 3 import ( 4 "fmt" 5 "net/http" 6 "reflect" 7 "testing" 8 ) 9 10 func TestAssociationMethods(t *testing.T) { 11 setup() 12 defer teardown() 13 14 mux.HandleFunc("/association_requests", func(w http.ResponseWriter, r *http.Request) { 15 switch { 16 case r.Method == "GET": 17 fmt.Fprintf(w, `[{"id":"1f", "username":"jollystranger"}, {"id":"2b", "username":"fredhamlet"}]`) 18 case r.Method == "POST": 19 fmt.Fprintf(w, `{ "uri": "http://chef/organizations/test/association_requests/1a" }`) 20 } 21 }) 22 23 mux.HandleFunc("/association_requests/1f", func(w http.ResponseWriter, r *http.Request) { 24 switch { 25 case r.Method == "GET": 26 fmt.Fprintf(w, `[ 27 { "user": {"username": "jollystranger"} 28 ]`) 29 case r.Method == "DELETE": 30 fmt.Fprintf(w, `{ 31 "id": "1f", 32 "orgname": "test", 33 "username": "jollystranger" 34 }`) 35 } 36 }) 37 38 // test Invite - Invite a user 39 request := Request{ 40 User: "jollystranger", 41 } 42 invite, err := client.Associations.Invite(request) 43 if err != nil { 44 t.Errorf("Associations.Invite returned error: %v", err) 45 } 46 associationWant := Association{ 47 Uri: "http://chef/organizations/test/association_requests/1a", 48 } 49 if !reflect.DeepEqual(invite, associationWant) { 50 t.Errorf("Associations.Invite returned %+v, want %+v", invite, associationWant) 51 } 52 53 // test ListInvites - return the existing invitations 54 invites, err := client.Associations.ListInvites() 55 if err != nil { 56 t.Errorf("Associations.List returned error: %v", err) 57 } 58 listWant := []Invite{ 59 Invite{Id: "1f", UserName: "jollystranger"}, 60 Invite{Id: "2b", UserName: "fredhamlet"}, 61 } 62 if !reflect.DeepEqual(invites, listWant) { 63 t.Errorf("Associations.InviteList returned %+v, want %+v", invites, listWant) 64 } 65 66 // test DeleteInvite 67 deleted, err := client.Associations.DeleteInvite("1f") 68 if err != nil { 69 t.Errorf("Associations.Get returned error: %v", err) 70 } 71 wantInvite := RescindInvite{ 72 Id: "1f", 73 Orgname: "test", 74 Username: "jollystranger", 75 } 76 if !reflect.DeepEqual(deleted, wantInvite) { 77 t.Errorf("Associations.RescindInvite returned %+v, want %+v", deleted, wantInvite) 78 } 79 80 // test InviteId 81 id, err := client.Associations.InviteId("fredhamlet") 82 if err != nil { 83 t.Errorf("Associations.InviteId returned error: %s", err.Error()) 84 } 85 wantId := "2b" 86 if !reflect.DeepEqual(id, wantId) { 87 t.Errorf("Associations.InviteId returned %+v, want %+v", id, wantId) 88 } 89 } 90 91 func TestOrgUserMethods(t *testing.T) { 92 setup() 93 defer teardown() 94 95 mux.HandleFunc("/users", func(w http.ResponseWriter, r *http.Request) { 96 switch { 97 case r.Method == "GET": 98 // []OrgUserListEntry 99 fmt.Fprintf(w, `[{ "user": {"username": "jollystranger"}}]`) 100 case r.Method == "POST": 101 // err only 102 } 103 }) 104 105 mux.HandleFunc("/users/jollystranger", func(w http.ResponseWriter, r *http.Request) { 106 switch { 107 case r.Method == "GET": 108 // OrgUser 109 fmt.Fprintf(w, `{ "username": "jollystranger", "email": "jolly.stranger@domain.io", "display_name":"jolly" }`) 110 case r.Method == "DELETE": 111 // OrgUser 112 fmt.Fprintf(w, `{ "username": "jollystranger", "email": "jolly.stranger@domain.io", "display_name":"jolly" }`) 113 } 114 }) 115 116 // test List the users 117 users, err := client.Associations.List() 118 if err != nil { 119 t.Errorf("Associations.List returned error: %v", err) 120 } 121 var a struct { 122 Username string `json:"username,omitempty"` 123 } 124 a.Username = "jollystranger" 125 var wuser OrgUserListEntry 126 wuser.User = a 127 wantUsers := []OrgUserListEntry{ 128 wuser, 129 } 130 if !reflect.DeepEqual(users, wantUsers) { 131 t.Errorf("Associations.List returned %+v, want %+v", users, wantUsers) 132 } 133 134 // test Add user 135 addme := AddNow{ 136 Username: "jollystranger", 137 } 138 err = client.Associations.Add(addme) 139 if err != nil { 140 t.Errorf("Associations.Add returned error: %v", err) 141 } 142 143 // test Get user details 144 user, err := client.Associations.Get("jollystranger") 145 if err != nil { 146 t.Errorf("Associations.Get returned error: %v", err) 147 } 148 wantUser := OrgUser{ 149 Username: "jollystranger", Email: "jolly.stranger@domain.io", DisplayName: "jolly", 150 } 151 if !reflect.DeepEqual(user, wantUser) { 152 t.Errorf("Associations.Get returned %+v, want %+v", user, wantUser) 153 } 154 155 // test Delete user details 156 delu, err := client.Associations.Delete("jollystranger") 157 if err != nil { 158 t.Errorf("Associations.Delete returned error: %v", err) 159 } 160 delUser := OrgUser{ 161 Username: "jollystranger", Email: "jolly.stranger@domain.io", DisplayName: "jolly", 162 } 163 if !reflect.DeepEqual(delu, delUser) { 164 t.Errorf("Associations.Delete returned %+v, want %+v", delu, delUser) 165 } 166 }