github.com/google/go-github/v33@v33.0.0/github/admin_users_test.go (about)

     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  	"reflect"
    14  	"testing"
    15  	"time"
    16  )
    17  
    18  func TestAdminUsers_Create(t *testing.T) {
    19  	client, mux, _, teardown := setup()
    20  	defer teardown()
    21  
    22  	mux.HandleFunc("/admin/users", func(w http.ResponseWriter, r *http.Request) {
    23  		v := new(createUserRequest)
    24  		json.NewDecoder(r.Body).Decode(v)
    25  
    26  		testMethod(t, r, "POST")
    27  		want := &createUserRequest{Login: String("github"), Email: String("email@domain.com")}
    28  		if !reflect.DeepEqual(v, want) {
    29  			t.Errorf("Request body = %+v, want %+v", v, want)
    30  		}
    31  
    32  		fmt.Fprint(w, `{"login":"github","id":1}`)
    33  	})
    34  
    35  	org, _, err := client.Admin.CreateUser(context.Background(), "github", "email@domain.com")
    36  	if err != nil {
    37  		t.Errorf("Admin.CreateUser returned error: %v", err)
    38  	}
    39  
    40  	want := &User{ID: Int64(1), Login: String("github")}
    41  	if !reflect.DeepEqual(org, want) {
    42  		t.Errorf("Admin.CreateUser returned %+v, want %+v", org, want)
    43  	}
    44  }
    45  
    46  func TestAdminUsers_Delete(t *testing.T) {
    47  	client, mux, _, teardown := setup()
    48  	defer teardown()
    49  
    50  	mux.HandleFunc("/admin/users/github", func(w http.ResponseWriter, r *http.Request) {
    51  		testMethod(t, r, "DELETE")
    52  	})
    53  
    54  	_, err := client.Admin.DeleteUser(context.Background(), "github")
    55  	if err != nil {
    56  		t.Errorf("Admin.DeleteUser returned error: %v", err)
    57  	}
    58  }
    59  
    60  func TestUserImpersonation_Create(t *testing.T) {
    61  	client, mux, _, teardown := setup()
    62  	defer teardown()
    63  
    64  	mux.HandleFunc("/admin/users/github/authorizations", func(w http.ResponseWriter, r *http.Request) {
    65  		testMethod(t, r, "POST")
    66  		testBody(t, r, `{"scopes":["repo"]}`+"\n")
    67  		fmt.Fprint(w, `{"id": 1234,
    68  		"url": "https://git.company.com/api/v3/authorizations/1234",
    69  		"app": {
    70  		  "name": "GitHub Site Administrator",
    71  		  "url": "https://docs.github.com/en/free-pro-team@latest/rest/reference/enterprise/users/",
    72  		  "client_id": "1234"
    73  		},
    74  		"token": "1234",
    75  		"hashed_token": "1234",
    76  		"token_last_eight": "1234",
    77  		"note": null,
    78  		"note_url": null,
    79  		"created_at": "2018-01-01T00:00:00Z",
    80  		"updated_at": "2018-01-01T00:00:00Z",
    81  		"scopes": [
    82  		  "repo"
    83  		],
    84  		"fingerprint": null}`)
    85  	})
    86  
    87  	opt := &ImpersonateUserOptions{Scopes: []string{"repo"}}
    88  	auth, _, err := client.Admin.CreateUserImpersonation(context.Background(), "github", opt)
    89  	if err != nil {
    90  		t.Errorf("Admin.CreateUserImpersonation returned error: %v", err)
    91  	}
    92  
    93  	date := Timestamp{Time: time.Date(2018, time.January, 1, 0, 0, 0, 0, time.UTC)}
    94  	want := &UserAuthorization{
    95  		ID:  Int64(1234),
    96  		URL: String("https://git.company.com/api/v3/authorizations/1234"),
    97  		App: &OAuthAPP{
    98  			Name:     String("GitHub Site Administrator"),
    99  			URL:      String("https://docs.github.com/en/free-pro-team@latest/rest/reference/enterprise/users/"),
   100  			ClientID: String("1234"),
   101  		},
   102  		Token:          String("1234"),
   103  		HashedToken:    String("1234"),
   104  		TokenLastEight: String("1234"),
   105  		Note:           nil,
   106  		NoteURL:        nil,
   107  		CreatedAt:      &date,
   108  		UpdatedAt:      &date,
   109  		Scopes:         []string{"repo"},
   110  		Fingerprint:    nil,
   111  	}
   112  	if !reflect.DeepEqual(auth, want) {
   113  		t.Errorf("Admin.CreateUserImpersonation returned %+v, want %+v", auth, want)
   114  	}
   115  }
   116  
   117  func TestUserImpersonation_Delete(t *testing.T) {
   118  	client, mux, _, teardown := setup()
   119  	defer teardown()
   120  
   121  	mux.HandleFunc("/admin/users/github/authorizations", func(w http.ResponseWriter, r *http.Request) {
   122  		testMethod(t, r, "DELETE")
   123  	})
   124  
   125  	_, err := client.Admin.DeleteUserImpersonation(context.Background(), "github")
   126  	if err != nil {
   127  		t.Errorf("Admin.DeleteUserImpersonation returned error: %v", err)
   128  	}
   129  }