...

Source file src/github.com/google/go-github/v47/github/admin_test.go

Documentation: github.com/google/go-github/v47/github

     1  // Copyright 2016 The go-github AUTHORS. All rights reserved.
     2  //
     3  // Use of this source code is governed by a BSD-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package github
     7  
     8  import (
     9  	"context"
    10  	"encoding/json"
    11  	"fmt"
    12  	"net/http"
    13  	"testing"
    14  
    15  	"github.com/google/go-cmp/cmp"
    16  )
    17  
    18  func TestAdminService_UpdateUserLDAPMapping(t *testing.T) {
    19  	client, mux, _, teardown := setup()
    20  	defer teardown()
    21  
    22  	input := &UserLDAPMapping{
    23  		LDAPDN: String("uid=asdf,ou=users,dc=github,dc=com"),
    24  	}
    25  
    26  	mux.HandleFunc("/admin/ldap/users/u/mapping", func(w http.ResponseWriter, r *http.Request) {
    27  		v := new(UserLDAPMapping)
    28  		json.NewDecoder(r.Body).Decode(v)
    29  
    30  		testMethod(t, r, "PATCH")
    31  		if !cmp.Equal(v, input) {
    32  			t.Errorf("Request body = %+v, want %+v", v, input)
    33  		}
    34  		fmt.Fprint(w, `{"id":1,"ldap_dn":"uid=asdf,ou=users,dc=github,dc=com"}`)
    35  	})
    36  
    37  	ctx := context.Background()
    38  	mapping, _, err := client.Admin.UpdateUserLDAPMapping(ctx, "u", input)
    39  	if err != nil {
    40  		t.Errorf("Admin.UpdateUserLDAPMapping returned error: %v", err)
    41  	}
    42  
    43  	want := &UserLDAPMapping{
    44  		ID:     Int64(1),
    45  		LDAPDN: String("uid=asdf,ou=users,dc=github,dc=com"),
    46  	}
    47  	if !cmp.Equal(mapping, want) {
    48  		t.Errorf("Admin.UpdateUserLDAPMapping returned %+v, want %+v", mapping, want)
    49  	}
    50  
    51  	const methodName = "UpdateUserLDAPMapping"
    52  	testBadOptions(t, methodName, func() (err error) {
    53  		_, _, err = client.Admin.UpdateUserLDAPMapping(ctx, "\n", input)
    54  		return err
    55  	})
    56  
    57  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
    58  		got, resp, err := client.Admin.UpdateUserLDAPMapping(ctx, "u", input)
    59  		if got != nil {
    60  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
    61  		}
    62  		return resp, err
    63  	})
    64  }
    65  
    66  func TestAdminService_UpdateTeamLDAPMapping(t *testing.T) {
    67  	client, mux, _, teardown := setup()
    68  	defer teardown()
    69  
    70  	input := &TeamLDAPMapping{
    71  		LDAPDN: String("cn=Enterprise Ops,ou=teams,dc=github,dc=com"),
    72  	}
    73  
    74  	mux.HandleFunc("/admin/ldap/teams/1/mapping", func(w http.ResponseWriter, r *http.Request) {
    75  		v := new(TeamLDAPMapping)
    76  		json.NewDecoder(r.Body).Decode(v)
    77  
    78  		testMethod(t, r, "PATCH")
    79  		if !cmp.Equal(v, input) {
    80  			t.Errorf("Request body = %+v, want %+v", v, input)
    81  		}
    82  		fmt.Fprint(w, `{"id":1,"ldap_dn":"cn=Enterprise Ops,ou=teams,dc=github,dc=com"}`)
    83  	})
    84  
    85  	ctx := context.Background()
    86  	mapping, _, err := client.Admin.UpdateTeamLDAPMapping(ctx, 1, input)
    87  	if err != nil {
    88  		t.Errorf("Admin.UpdateTeamLDAPMapping returned error: %v", err)
    89  	}
    90  
    91  	want := &TeamLDAPMapping{
    92  		ID:     Int64(1),
    93  		LDAPDN: String("cn=Enterprise Ops,ou=teams,dc=github,dc=com"),
    94  	}
    95  	if !cmp.Equal(mapping, want) {
    96  		t.Errorf("Admin.UpdateTeamLDAPMapping returned %+v, want %+v", mapping, want)
    97  	}
    98  
    99  	const methodName = "UpdateTeamLDAPMapping"
   100  	testBadOptions(t, methodName, func() (err error) {
   101  		_, _, err = client.Admin.UpdateTeamLDAPMapping(ctx, -1, input)
   102  		return err
   103  	})
   104  
   105  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   106  		got, resp, err := client.Admin.UpdateTeamLDAPMapping(ctx, 1, input)
   107  		if got != nil {
   108  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   109  		}
   110  		return resp, err
   111  	})
   112  }
   113  
   114  func TestAdminService_TeamLDAPMapping_String(t *testing.T) {
   115  	v := &TeamLDAPMapping{
   116  		ID:              Int64(1),
   117  		LDAPDN:          String("a"),
   118  		URL:             String("b"),
   119  		Name:            String("c"),
   120  		Slug:            String("d"),
   121  		Description:     String("e"),
   122  		Privacy:         String("f"),
   123  		Permission:      String("g"),
   124  		MembersURL:      String("h"),
   125  		RepositoriesURL: String("i"),
   126  	}
   127  
   128  	want := `github.TeamLDAPMapping{ID:1, LDAPDN:"a", URL:"b", Name:"c", Slug:"d", Description:"e", Privacy:"f", Permission:"g", MembersURL:"h", RepositoriesURL:"i"}`
   129  	if got := v.String(); got != want {
   130  		t.Errorf("TeamLDAPMapping.String = `%v`, want `%v`", got, want)
   131  	}
   132  }
   133  
   134  func TestAdminService_UserLDAPMapping_String(t *testing.T) {
   135  	v := &UserLDAPMapping{
   136  		ID:                Int64(1),
   137  		LDAPDN:            String("a"),
   138  		Login:             String("b"),
   139  		AvatarURL:         String("c"),
   140  		GravatarID:        String("d"),
   141  		Type:              String("e"),
   142  		SiteAdmin:         Bool(true),
   143  		URL:               String("f"),
   144  		EventsURL:         String("g"),
   145  		FollowingURL:      String("h"),
   146  		FollowersURL:      String("i"),
   147  		GistsURL:          String("j"),
   148  		OrganizationsURL:  String("k"),
   149  		ReceivedEventsURL: String("l"),
   150  		ReposURL:          String("m"),
   151  		StarredURL:        String("n"),
   152  		SubscriptionsURL:  String("o"),
   153  	}
   154  
   155  	want := `github.UserLDAPMapping{ID:1, LDAPDN:"a", Login:"b", AvatarURL:"c", GravatarID:"d", Type:"e", SiteAdmin:true, URL:"f", EventsURL:"g", FollowingURL:"h", FollowersURL:"i", GistsURL:"j", OrganizationsURL:"k", ReceivedEventsURL:"l", ReposURL:"m", StarredURL:"n", SubscriptionsURL:"o"}`
   156  	if got := v.String(); got != want {
   157  		t.Errorf("UserLDAPMapping.String = `%v`, want `%v`", got, want)
   158  	}
   159  }
   160  
   161  func TestTeamLDAPMapping_Marshal(t *testing.T) {
   162  	testJSONMarshal(t, &TeamLDAPMapping{}, "{}")
   163  
   164  	u := &TeamLDAPMapping{
   165  		ID:              Int64(1),
   166  		LDAPDN:          String("ldapdn"),
   167  		URL:             String("u"),
   168  		Name:            String("n"),
   169  		Slug:            String("s"),
   170  		Description:     String("d"),
   171  		Privacy:         String("p"),
   172  		Permission:      String("per"),
   173  		MembersURL:      String("mu"),
   174  		RepositoriesURL: String("ru"),
   175  	}
   176  
   177  	want := `{
   178  		"id": 1,
   179  		"ldap_dn": "ldapdn",
   180  		"url": "u",
   181  		"name": "n",
   182  		"slug": "s",
   183  		"description": "d",
   184  		"privacy": "p",
   185  		"permission": "per",
   186  		"members_url": "mu",
   187  		"repositories_url": "ru"
   188  	}`
   189  
   190  	testJSONMarshal(t, u, want)
   191  }
   192  
   193  func TestUserLDAPMapping_Marshal(t *testing.T) {
   194  	testJSONMarshal(t, &UserLDAPMapping{}, "{}")
   195  
   196  	u := &UserLDAPMapping{
   197  		ID:                Int64(1),
   198  		LDAPDN:            String("ldapdn"),
   199  		Login:             String("l"),
   200  		AvatarURL:         String("au"),
   201  		GravatarID:        String("gi"),
   202  		Type:              String("t"),
   203  		SiteAdmin:         Bool(true),
   204  		URL:               String("u"),
   205  		EventsURL:         String("eu"),
   206  		FollowingURL:      String("fu"),
   207  		FollowersURL:      String("fu"),
   208  		GistsURL:          String("gu"),
   209  		OrganizationsURL:  String("ou"),
   210  		ReceivedEventsURL: String("reu"),
   211  		ReposURL:          String("ru"),
   212  		StarredURL:        String("su"),
   213  		SubscriptionsURL:  String("subu"),
   214  	}
   215  
   216  	want := `{
   217  		"id": 1,
   218  		"ldap_dn": "ldapdn",
   219  		"login": "l",
   220  		"avatar_url": "au",
   221  		"gravatar_id": "gi",
   222  		"type": "t",
   223  		"site_admin": true,
   224  		"url": "u",
   225  		"events_url": "eu",
   226  		"following_url": "fu",
   227  		"followers_url": "fu",
   228  		"gists_url": "gu",
   229  		"organizations_url": "ou",
   230  		"received_events_url": "reu",
   231  		"repos_url": "ru",
   232  		"starred_url": "su",
   233  		"subscriptions_url": "subu"
   234  	}`
   235  
   236  	testJSONMarshal(t, u, want)
   237  }
   238  
   239  func TestEnterprise_Marshal(t *testing.T) {
   240  	testJSONMarshal(t, &Enterprise{}, "{}")
   241  
   242  	u := &Enterprise{
   243  		ID:          Int(1),
   244  		Slug:        String("s"),
   245  		Name:        String("n"),
   246  		NodeID:      String("nid"),
   247  		AvatarURL:   String("au"),
   248  		Description: String("d"),
   249  		WebsiteURL:  String("wu"),
   250  		HTMLURL:     String("hu"),
   251  		CreatedAt:   &Timestamp{referenceTime},
   252  		UpdatedAt:   &Timestamp{referenceTime},
   253  	}
   254  
   255  	want := `{
   256  		"id": 1,
   257  		"slug": "s",
   258  		"name": "n",
   259  		"node_id": "nid",
   260  		"avatar_url": "au",
   261  		"description": "d",
   262  		"website_url": "wu",
   263  		"html_url": "hu",
   264  		"created_at": ` + referenceTimeStr + `,
   265  		"updated_at": ` + referenceTimeStr + `
   266  	}`
   267  
   268  	testJSONMarshal(t, u, want)
   269  }
   270  

View as plain text