...

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

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

     1  // Copyright 2019 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  	"time"
    15  
    16  	"github.com/google/go-cmp/cmp"
    17  )
    18  
    19  func TestAdminUsers_Create(t *testing.T) {
    20  	client, mux, _, teardown := setup()
    21  	defer teardown()
    22  
    23  	mux.HandleFunc("/admin/users", func(w http.ResponseWriter, r *http.Request) {
    24  		v := new(createUserRequest)
    25  		json.NewDecoder(r.Body).Decode(v)
    26  
    27  		testMethod(t, r, "POST")
    28  		want := &createUserRequest{Login: String("github"), Email: String("email@domain.com")}
    29  		if !cmp.Equal(v, want) {
    30  			t.Errorf("Request body = %+v, want %+v", v, want)
    31  		}
    32  
    33  		fmt.Fprint(w, `{"login":"github","id":1}`)
    34  	})
    35  
    36  	ctx := context.Background()
    37  	org, _, err := client.Admin.CreateUser(ctx, "github", "email@domain.com")
    38  	if err != nil {
    39  		t.Errorf("Admin.CreateUser returned error: %v", err)
    40  	}
    41  
    42  	want := &User{ID: Int64(1), Login: String("github")}
    43  	if !cmp.Equal(org, want) {
    44  		t.Errorf("Admin.CreateUser returned %+v, want %+v", org, want)
    45  	}
    46  
    47  	const methodName = "CreateUser"
    48  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
    49  		got, resp, err := client.Admin.CreateUser(ctx, "github", "email@domain.com")
    50  		if got != nil {
    51  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
    52  		}
    53  		return resp, err
    54  	})
    55  }
    56  
    57  func TestAdminUsers_Delete(t *testing.T) {
    58  	client, mux, _, teardown := setup()
    59  	defer teardown()
    60  
    61  	mux.HandleFunc("/admin/users/github", func(w http.ResponseWriter, r *http.Request) {
    62  		testMethod(t, r, "DELETE")
    63  	})
    64  
    65  	ctx := context.Background()
    66  	_, err := client.Admin.DeleteUser(ctx, "github")
    67  	if err != nil {
    68  		t.Errorf("Admin.DeleteUser returned error: %v", err)
    69  	}
    70  
    71  	const methodName = "DeleteUser"
    72  	testBadOptions(t, methodName, func() (err error) {
    73  		_, err = client.Admin.DeleteUser(ctx, "\n")
    74  		return err
    75  	})
    76  
    77  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
    78  		return client.Admin.DeleteUser(ctx, "github")
    79  	})
    80  }
    81  
    82  func TestUserImpersonation_Create(t *testing.T) {
    83  	client, mux, _, teardown := setup()
    84  	defer teardown()
    85  
    86  	mux.HandleFunc("/admin/users/github/authorizations", func(w http.ResponseWriter, r *http.Request) {
    87  		testMethod(t, r, "POST")
    88  		testBody(t, r, `{"scopes":["repo"]}`+"\n")
    89  		fmt.Fprint(w, `{"id": 1234,
    90  		"url": "https://git.company.com/api/v3/authorizations/1234",
    91  		"app": {
    92  		  "name": "GitHub Site Administrator",
    93  		  "url": "https://docs.github.com/en/rest/enterprise/users/",
    94  		  "client_id": "1234"
    95  		},
    96  		"token": "1234",
    97  		"hashed_token": "1234",
    98  		"token_last_eight": "1234",
    99  		"note": null,
   100  		"note_url": null,
   101  		"created_at": "2018-01-01T00:00:00Z",
   102  		"updated_at": "2018-01-01T00:00:00Z",
   103  		"scopes": [
   104  		  "repo"
   105  		],
   106  		"fingerprint": null}`)
   107  	})
   108  
   109  	opt := &ImpersonateUserOptions{Scopes: []string{"repo"}}
   110  	ctx := context.Background()
   111  	auth, _, err := client.Admin.CreateUserImpersonation(ctx, "github", opt)
   112  	if err != nil {
   113  		t.Errorf("Admin.CreateUserImpersonation returned error: %v", err)
   114  	}
   115  
   116  	date := Timestamp{Time: time.Date(2018, time.January, 1, 0, 0, 0, 0, time.UTC)}
   117  	want := &UserAuthorization{
   118  		ID:  Int64(1234),
   119  		URL: String("https://git.company.com/api/v3/authorizations/1234"),
   120  		App: &OAuthAPP{
   121  			Name:     String("GitHub Site Administrator"),
   122  			URL:      String("https://docs.github.com/en/rest/enterprise/users/"),
   123  			ClientID: String("1234"),
   124  		},
   125  		Token:          String("1234"),
   126  		HashedToken:    String("1234"),
   127  		TokenLastEight: String("1234"),
   128  		Note:           nil,
   129  		NoteURL:        nil,
   130  		CreatedAt:      &date,
   131  		UpdatedAt:      &date,
   132  		Scopes:         []string{"repo"},
   133  		Fingerprint:    nil,
   134  	}
   135  	if !cmp.Equal(auth, want) {
   136  		t.Errorf("Admin.CreateUserImpersonation returned %+v, want %+v", auth, want)
   137  	}
   138  
   139  	const methodName = "CreateUserImpersonation"
   140  	testBadOptions(t, methodName, func() (err error) {
   141  		_, _, err = client.Admin.CreateUserImpersonation(ctx, "\n", opt)
   142  		return err
   143  	})
   144  
   145  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   146  		got, resp, err := client.Admin.CreateUserImpersonation(ctx, "github", opt)
   147  		if got != nil {
   148  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   149  		}
   150  		return resp, err
   151  	})
   152  }
   153  
   154  func TestUserImpersonation_Delete(t *testing.T) {
   155  	client, mux, _, teardown := setup()
   156  	defer teardown()
   157  
   158  	mux.HandleFunc("/admin/users/github/authorizations", func(w http.ResponseWriter, r *http.Request) {
   159  		testMethod(t, r, "DELETE")
   160  	})
   161  
   162  	ctx := context.Background()
   163  	_, err := client.Admin.DeleteUserImpersonation(ctx, "github")
   164  	if err != nil {
   165  		t.Errorf("Admin.DeleteUserImpersonation returned error: %v", err)
   166  	}
   167  
   168  	const methodName = "DeleteUserImpersonation"
   169  	testBadOptions(t, methodName, func() (err error) {
   170  		_, err = client.Admin.DeleteUserImpersonation(ctx, "\n")
   171  		return err
   172  	})
   173  
   174  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   175  		return client.Admin.DeleteUserImpersonation(ctx, "github")
   176  	})
   177  }
   178  
   179  func TestCreateUserRequest_Marshal(t *testing.T) {
   180  	testJSONMarshal(t, &createUserRequest{}, "{}")
   181  
   182  	u := &createUserRequest{
   183  		Login: String("l"),
   184  		Email: String("e"),
   185  	}
   186  
   187  	want := `{
   188  		"login": "l",
   189  		"email": "e"
   190  	}`
   191  
   192  	testJSONMarshal(t, u, want)
   193  }
   194  
   195  func TestImpersonateUserOptions_Marshal(t *testing.T) {
   196  	testJSONMarshal(t, &ImpersonateUserOptions{}, "{}")
   197  
   198  	u := &ImpersonateUserOptions{
   199  		Scopes: []string{
   200  			"s",
   201  		},
   202  	}
   203  
   204  	want := `{
   205  		"scopes": ["s"]
   206  	}`
   207  
   208  	testJSONMarshal(t, u, want)
   209  }
   210  
   211  func TestOAuthAPP_Marshal(t *testing.T) {
   212  	testJSONMarshal(t, &OAuthAPP{}, "{}")
   213  
   214  	u := &OAuthAPP{
   215  		URL:      String("u"),
   216  		Name:     String("n"),
   217  		ClientID: String("cid"),
   218  	}
   219  
   220  	want := `{
   221  		"url": "u",
   222  		"name": "n",
   223  		"client_id": "cid"
   224  	}`
   225  
   226  	testJSONMarshal(t, u, want)
   227  }
   228  
   229  func TestUserAuthorization_Marshal(t *testing.T) {
   230  	testJSONMarshal(t, &UserAuthorization{}, "{}")
   231  
   232  	u := &UserAuthorization{
   233  		ID:  Int64(1),
   234  		URL: String("u"),
   235  		Scopes: []string{
   236  			"s",
   237  		},
   238  		Token:          String("t"),
   239  		TokenLastEight: String("tle"),
   240  		HashedToken:    String("ht"),
   241  		App: &OAuthAPP{
   242  			URL:      String("u"),
   243  			Name:     String("n"),
   244  			ClientID: String("cid"),
   245  		},
   246  		Note:        String("n"),
   247  		NoteURL:     String("nu"),
   248  		UpdatedAt:   &Timestamp{referenceTime},
   249  		CreatedAt:   &Timestamp{referenceTime},
   250  		Fingerprint: String("f"),
   251  	}
   252  
   253  	want := `{
   254  		"id": 1,
   255  		"url": "u",
   256  		"scopes": ["s"],
   257  		"token": "t",
   258  		"token_last_eight": "tle",
   259  		"hashed_token": "ht",
   260  		"app": {
   261  			"url": "u",
   262  			"name": "n",
   263  			"client_id": "cid"
   264  		},
   265  		"note": "n",
   266  		"note_url": "nu",
   267  		"updated_at": ` + referenceTimeStr + `,
   268  		"created_at": ` + referenceTimeStr + `,
   269  		"fingerprint": "f"
   270  	}`
   271  
   272  	testJSONMarshal(t, u, want)
   273  }
   274  

View as plain text