// Copyright 2013 Google. All rights reserved.
|
|
//
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file or at
|
|
// https://developers.google.com/open-source/licenses/bsd
|
|
|
|
package github
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func TestOrganizationsService_List_authenticatedUser(t *testing.T) {
|
|
setup()
|
|
defer teardown()
|
|
|
|
mux.HandleFunc("/user/orgs", func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != "GET" {
|
|
t.Errorf("Request method = %v, want %v", r.Method, "GET")
|
|
}
|
|
fmt.Fprint(w, `[{"id":1},{"id":2}]`)
|
|
})
|
|
|
|
orgs, err := client.Organizations.List("")
|
|
if err != nil {
|
|
t.Errorf("Organizations.List returned error: %v", err)
|
|
}
|
|
|
|
want := []Organization{Organization{ID: 1}, Organization{ID: 2}}
|
|
if !reflect.DeepEqual(orgs, want) {
|
|
t.Errorf("Organizations.List returned %+v, want %+v", orgs, want)
|
|
}
|
|
}
|
|
|
|
func TestOrganizationsService_List_specifiedUser(t *testing.T) {
|
|
setup()
|
|
defer teardown()
|
|
|
|
mux.HandleFunc("/users/u/orgs", func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != "GET" {
|
|
t.Errorf("Request method = %v, want %v", r.Method, "GET")
|
|
}
|
|
fmt.Fprint(w, `[{"id":1},{"id":2}]`)
|
|
})
|
|
|
|
orgs, err := client.Organizations.List("u")
|
|
if err != nil {
|
|
t.Errorf("Organizations.List returned error: %v", err)
|
|
}
|
|
|
|
want := []Organization{Organization{ID: 1}, Organization{ID: 2}}
|
|
if !reflect.DeepEqual(orgs, want) {
|
|
t.Errorf("Organizations.List returned %+v, want %+v", orgs, want)
|
|
}
|
|
}
|
|
|
|
func TestOrganizationsService_Get(t *testing.T) {
|
|
setup()
|
|
defer teardown()
|
|
|
|
mux.HandleFunc("/orgs/o", func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != "GET" {
|
|
t.Errorf("Request method = %v, want %v", r.Method, "GET")
|
|
}
|
|
fmt.Fprint(w, `{"id":1, "login":"l", "url":"u", "avatar_url": "a", "location":"l"}`)
|
|
})
|
|
|
|
org, err := client.Organizations.Get("o")
|
|
if err != nil {
|
|
t.Errorf("Organizations.Get returned error: %v", err)
|
|
}
|
|
|
|
want := &Organization{ID: 1, Login: "l", URL: "u", AvatarURL: "a", Location: "l"}
|
|
if !reflect.DeepEqual(org, want) {
|
|
t.Errorf("Organizations.Get returned %+v, want %+v", org, want)
|
|
}
|
|
}
|
|
|
|
func TestOrganizationsService_Edit(t *testing.T) {
|
|
setup()
|
|
defer teardown()
|
|
|
|
input := &Organization{Login: "l"}
|
|
|
|
mux.HandleFunc("/orgs/o", func(w http.ResponseWriter, r *http.Request) {
|
|
v := new(Organization)
|
|
json.NewDecoder(r.Body).Decode(v)
|
|
|
|
if r.Method != "PATCH" {
|
|
t.Errorf("Request method = %v, want %v", r.Method, "GET")
|
|
}
|
|
if !reflect.DeepEqual(v, input) {
|
|
t.Errorf("Request body = %+v, want %+v", v, input)
|
|
}
|
|
|
|
fmt.Fprint(w, `{"id":1}`)
|
|
})
|
|
|
|
org, err := client.Organizations.Edit("o", input)
|
|
if err != nil {
|
|
t.Errorf("Organizations.Edit returned error: %v", err)
|
|
}
|
|
|
|
want := &Organization{ID: 1}
|
|
if !reflect.DeepEqual(org, want) {
|
|
t.Errorf("Organizations.Edit returned %+v, want %+v", org, want)
|
|
}
|
|
}
|
|
|
|
func TestOrganizationsService_ListMembers(t *testing.T) {
|
|
setup()
|
|
defer teardown()
|
|
|
|
mux.HandleFunc("/orgs/o/members", func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != "GET" {
|
|
t.Errorf("Request method = %v, want %v", r.Method, "GET")
|
|
}
|
|
fmt.Fprint(w, `[{"id":1}]`)
|
|
})
|
|
|
|
members, err := client.Organizations.ListMembers("o")
|
|
if err != nil {
|
|
t.Errorf("Organizations.ListMembers returned error: %v", err)
|
|
}
|
|
|
|
want := []User{User{ID: 1}}
|
|
if !reflect.DeepEqual(members, want) {
|
|
t.Errorf("Organizations.ListMembers returned %+v, want %+v", members, want)
|
|
}
|
|
}
|
|
|
|
func TestOrganizationsService_ListPublicMembers(t *testing.T) {
|
|
setup()
|
|
defer teardown()
|
|
|
|
mux.HandleFunc("/orgs/o/public_members", func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != "GET" {
|
|
t.Errorf("Request method = %v, want %v", r.Method, "GET")
|
|
}
|
|
fmt.Fprint(w, `[{"id":1}]`)
|
|
})
|
|
|
|
members, err := client.Organizations.ListPublicMembers("o")
|
|
if err != nil {
|
|
t.Errorf("Organizations.ListPublicMembers returned error: %v", err)
|
|
}
|
|
|
|
want := []User{User{ID: 1}}
|
|
if !reflect.DeepEqual(members, want) {
|
|
t.Errorf("Organizations.ListPublicMembers returned %+v, want %+v", members, want)
|
|
}
|
|
}
|
|
|
|
func TestOrganizationsService_ListTeams(t *testing.T) {
|
|
setup()
|
|
defer teardown()
|
|
|
|
mux.HandleFunc("/orgs/o/teams", func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != "GET" {
|
|
t.Errorf("Request method = %v, want %v", r.Method, "GET")
|
|
}
|
|
fmt.Fprint(w, `[{"id":1}]`)
|
|
})
|
|
|
|
teams, err := client.Organizations.ListTeams("o")
|
|
if err != nil {
|
|
t.Errorf("Organizations.ListTeams returned error: %v", err)
|
|
}
|
|
|
|
want := []Team{Team{ID: 1}}
|
|
if !reflect.DeepEqual(teams, want) {
|
|
t.Errorf("Organizations.ListTeams returned %+v, want %+v", teams, want)
|
|
}
|
|
}
|
|
|
|
func TestOrganizationsService_AddTeamMember(t *testing.T) {
|
|
setup()
|
|
defer teardown()
|
|
|
|
mux.HandleFunc("/teams/1/members/u", func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != "PUT" {
|
|
t.Errorf("Request method = %v, want %v", r.Method, "GET")
|
|
}
|
|
})
|
|
|
|
err := client.Organizations.AddTeamMember(1, "u")
|
|
if err != nil {
|
|
t.Errorf("Organizations.AddTeamMember returned error: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestOrganizationsService_RemoveTeamMember(t *testing.T) {
|
|
setup()
|
|
defer teardown()
|
|
|
|
mux.HandleFunc("/teams/1/members/u", func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != "DELETE" {
|
|
t.Errorf("Request method = %v, want %v", r.Method, "GET")
|
|
}
|
|
})
|
|
|
|
err := client.Organizations.RemoveTeamMember(1, "u")
|
|
if err != nil {
|
|
t.Errorf("Organizations.RemoveTeamMember returned error: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestOrganizationsService_PublicizeMembership(t *testing.T) {
|
|
setup()
|
|
defer teardown()
|
|
|
|
mux.HandleFunc("/orgs/o/public_members/u", func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != "PUT" {
|
|
t.Errorf("Request method = %v, want %v", r.Method, "GET")
|
|
}
|
|
})
|
|
|
|
err := client.Organizations.PublicizeMembership("o", "u")
|
|
if err != nil {
|
|
t.Errorf("Organizations.PublicizeMembership returned error: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestOrganizationsService_ConcealMembership(t *testing.T) {
|
|
setup()
|
|
defer teardown()
|
|
|
|
mux.HandleFunc("/orgs/o/public_members/u", func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != "DELETE" {
|
|
t.Errorf("Request method = %v, want %v", r.Method, "GET")
|
|
}
|
|
})
|
|
|
|
err := client.Organizations.ConcealMembership("o", "u")
|
|
if err != nil {
|
|
t.Errorf("Organizations.ConcealMembership returned error: %v", err)
|
|
}
|
|
}
|