github.com/google/go-github/v69@v69.2.0/github/authorizations_test.go (about)

     1  // Copyright 2015 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  	"fmt"
    11  	"net/http"
    12  	"testing"
    13  
    14  	"github.com/google/go-cmp/cmp"
    15  )
    16  
    17  func TestAuthorizationsService_Check(t *testing.T) {
    18  	t.Parallel()
    19  	client, mux, _ := setup(t)
    20  
    21  	mux.HandleFunc("/applications/id/token", func(w http.ResponseWriter, r *http.Request) {
    22  		testMethod(t, r, "POST")
    23  		testBody(t, r, `{"access_token":"a"}`+"\n")
    24  		testHeader(t, r, "Accept", mediaTypeOAuthAppPreview)
    25  		fmt.Fprint(w, `{"id":1}`)
    26  	})
    27  
    28  	ctx := context.Background()
    29  	got, _, err := client.Authorizations.Check(ctx, "id", "a")
    30  	if err != nil {
    31  		t.Errorf("Authorizations.Check returned error: %v", err)
    32  	}
    33  
    34  	want := &Authorization{ID: Ptr(int64(1))}
    35  	if !cmp.Equal(got, want) {
    36  		t.Errorf("Authorizations.Check returned auth %+v, want %+v", got, want)
    37  	}
    38  
    39  	const methodName = "Check"
    40  	testBadOptions(t, methodName, func() (err error) {
    41  		_, _, err = client.Authorizations.Check(ctx, "\n", "\n")
    42  		return err
    43  	})
    44  
    45  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
    46  		got, resp, err := client.Authorizations.Check(ctx, "id", "a")
    47  		if got != nil {
    48  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
    49  		}
    50  		return resp, err
    51  	})
    52  }
    53  
    54  func TestAuthorizationsService_Reset(t *testing.T) {
    55  	t.Parallel()
    56  	client, mux, _ := setup(t)
    57  
    58  	mux.HandleFunc("/applications/id/token", func(w http.ResponseWriter, r *http.Request) {
    59  		testMethod(t, r, "PATCH")
    60  		testBody(t, r, `{"access_token":"a"}`+"\n")
    61  		testHeader(t, r, "Accept", mediaTypeOAuthAppPreview)
    62  		fmt.Fprint(w, `{"ID":1}`)
    63  	})
    64  
    65  	ctx := context.Background()
    66  	got, _, err := client.Authorizations.Reset(ctx, "id", "a")
    67  	if err != nil {
    68  		t.Errorf("Authorizations.Reset returned error: %v", err)
    69  	}
    70  
    71  	want := &Authorization{ID: Ptr(int64(1))}
    72  	if !cmp.Equal(got, want) {
    73  		t.Errorf("Authorizations.Reset returned auth %+v, want %+v", got, want)
    74  	}
    75  
    76  	const methodName = "Reset"
    77  	testBadOptions(t, methodName, func() (err error) {
    78  		_, _, err = client.Authorizations.Reset(ctx, "\n", "\n")
    79  		return err
    80  	})
    81  
    82  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
    83  		got, resp, err := client.Authorizations.Reset(ctx, "id", "a")
    84  		if got != nil {
    85  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
    86  		}
    87  		return resp, err
    88  	})
    89  }
    90  
    91  func TestAuthorizationsService_Revoke(t *testing.T) {
    92  	t.Parallel()
    93  	client, mux, _ := setup(t)
    94  
    95  	mux.HandleFunc("/applications/id/token", func(w http.ResponseWriter, r *http.Request) {
    96  		testMethod(t, r, "DELETE")
    97  		testBody(t, r, `{"access_token":"a"}`+"\n")
    98  		testHeader(t, r, "Accept", mediaTypeOAuthAppPreview)
    99  		w.WriteHeader(http.StatusNoContent)
   100  	})
   101  
   102  	ctx := context.Background()
   103  	_, err := client.Authorizations.Revoke(ctx, "id", "a")
   104  	if err != nil {
   105  		t.Errorf("Authorizations.Revoke returned error: %v", err)
   106  	}
   107  
   108  	const methodName = "Revoke"
   109  	testBadOptions(t, methodName, func() (err error) {
   110  		_, err = client.Authorizations.Revoke(ctx, "\n", "\n")
   111  		return err
   112  	})
   113  
   114  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   115  		return client.Authorizations.Revoke(ctx, "id", "a")
   116  	})
   117  }
   118  
   119  func TestDeleteGrant(t *testing.T) {
   120  	t.Parallel()
   121  	client, mux, _ := setup(t)
   122  
   123  	mux.HandleFunc("/applications/id/grant", func(w http.ResponseWriter, r *http.Request) {
   124  		testMethod(t, r, "DELETE")
   125  		testBody(t, r, `{"access_token":"a"}`+"\n")
   126  		testHeader(t, r, "Accept", mediaTypeOAuthAppPreview)
   127  	})
   128  
   129  	ctx := context.Background()
   130  	_, err := client.Authorizations.DeleteGrant(ctx, "id", "a")
   131  	if err != nil {
   132  		t.Errorf("OAuthAuthorizations.DeleteGrant returned error: %v", err)
   133  	}
   134  
   135  	const methodName = "DeleteGrant"
   136  	testBadOptions(t, methodName, func() (err error) {
   137  		_, err = client.Authorizations.DeleteGrant(ctx, "\n", "\n")
   138  		return err
   139  	})
   140  
   141  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   142  		return client.Authorizations.DeleteGrant(ctx, "id", "a")
   143  	})
   144  }
   145  
   146  func TestAuthorizationsService_CreateImpersonation(t *testing.T) {
   147  	t.Parallel()
   148  	client, mux, _ := setup(t)
   149  
   150  	mux.HandleFunc("/admin/users/u/authorizations", func(w http.ResponseWriter, r *http.Request) {
   151  		testMethod(t, r, "POST")
   152  		fmt.Fprint(w, `{"id":1}`)
   153  	})
   154  
   155  	req := &AuthorizationRequest{Scopes: []Scope{ScopePublicRepo}}
   156  	ctx := context.Background()
   157  	got, _, err := client.Authorizations.CreateImpersonation(ctx, "u", req)
   158  	if err != nil {
   159  		t.Errorf("Authorizations.CreateImpersonation returned error: %+v", err)
   160  	}
   161  
   162  	want := &Authorization{ID: Ptr(int64(1))}
   163  	if !cmp.Equal(got, want) {
   164  		t.Errorf("Authorizations.CreateImpersonation returned %+v, want %+v", *got.ID, *want.ID)
   165  	}
   166  
   167  	const methodName = "CreateImpersonation"
   168  	testBadOptions(t, methodName, func() (err error) {
   169  		_, _, err = client.Authorizations.CreateImpersonation(ctx, "\n", req)
   170  		return err
   171  	})
   172  
   173  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   174  		got, resp, err := client.Authorizations.CreateImpersonation(ctx, "u", req)
   175  		if got != nil {
   176  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   177  		}
   178  		return resp, err
   179  	})
   180  }
   181  
   182  func TestAuthorizationsService_DeleteImpersonation(t *testing.T) {
   183  	t.Parallel()
   184  	client, mux, _ := setup(t)
   185  
   186  	mux.HandleFunc("/admin/users/u/authorizations", func(w http.ResponseWriter, r *http.Request) {
   187  		testMethod(t, r, "DELETE")
   188  	})
   189  
   190  	ctx := context.Background()
   191  	_, err := client.Authorizations.DeleteImpersonation(ctx, "u")
   192  	if err != nil {
   193  		t.Errorf("Authorizations.DeleteImpersonation returned error: %+v", err)
   194  	}
   195  
   196  	const methodName = "DeleteImpersonation"
   197  	testBadOptions(t, methodName, func() (err error) {
   198  		_, err = client.Authorizations.DeleteImpersonation(ctx, "\n")
   199  		return err
   200  	})
   201  
   202  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   203  		return client.Authorizations.DeleteImpersonation(ctx, "u")
   204  	})
   205  }
   206  
   207  func TestAuthorizationUpdateRequest_Marshal(t *testing.T) {
   208  	t.Parallel()
   209  	testJSONMarshal(t, &AuthorizationUpdateRequest{}, "{}")
   210  
   211  	u := &AuthorizationUpdateRequest{
   212  		Scopes:       []string{"s"},
   213  		AddScopes:    []string{"a"},
   214  		RemoveScopes: []string{"r"},
   215  		Note:         Ptr("n"),
   216  		NoteURL:      Ptr("nu"),
   217  		Fingerprint:  Ptr("f"),
   218  	}
   219  
   220  	want := `{
   221  		"scopes": ["s"],
   222  		"add_scopes": ["a"],
   223  		"remove_scopes": ["r"],
   224  		"note": "n",
   225  		"note_url": "nu",
   226  		"fingerprint": "f"
   227  	}`
   228  
   229  	testJSONMarshal(t, u, want)
   230  }
   231  
   232  func TestAuthorizationRequest_Marshal(t *testing.T) {
   233  	t.Parallel()
   234  	testJSONMarshal(t, &AuthorizationRequest{}, "{}")
   235  
   236  	u := &AuthorizationRequest{
   237  		Scopes:       []Scope{"s"},
   238  		ClientID:     Ptr("cid"),
   239  		ClientSecret: Ptr("cs"),
   240  		Note:         Ptr("n"),
   241  		NoteURL:      Ptr("nu"),
   242  		Fingerprint:  Ptr("f"),
   243  	}
   244  
   245  	want := `{
   246  		"scopes": ["s"],
   247  		"client_id": "cid",
   248  		"client_secret": "cs",
   249  		"note": "n",
   250  		"note_url": "nu",
   251  		"fingerprint": "f"
   252  	}`
   253  
   254  	testJSONMarshal(t, u, want)
   255  }
   256  
   257  func TestAuthorizationApp_Marshal(t *testing.T) {
   258  	t.Parallel()
   259  	testJSONMarshal(t, &AuthorizationApp{}, "{}")
   260  
   261  	u := &AuthorizationApp{
   262  		URL:      Ptr("u"),
   263  		Name:     Ptr("n"),
   264  		ClientID: Ptr("cid"),
   265  	}
   266  
   267  	want := `{
   268  		"url": "u",
   269  		"name": "n",
   270  		"client_id": "cid"
   271  	}`
   272  
   273  	testJSONMarshal(t, u, want)
   274  }
   275  
   276  func TestGrant_Marshal(t *testing.T) {
   277  	t.Parallel()
   278  	testJSONMarshal(t, &Grant{}, "{}")
   279  
   280  	u := &Grant{
   281  		ID:  Ptr(int64(1)),
   282  		URL: Ptr("u"),
   283  		App: &AuthorizationApp{
   284  			URL:      Ptr("u"),
   285  			Name:     Ptr("n"),
   286  			ClientID: Ptr("cid"),
   287  		},
   288  		CreatedAt: &Timestamp{referenceTime},
   289  		UpdatedAt: &Timestamp{referenceTime},
   290  		Scopes:    []string{"s"},
   291  	}
   292  
   293  	want := `{
   294  		"id": 1,
   295  		"url": "u",
   296  		"app": {
   297  			"url": "u",
   298  			"name": "n",
   299  			"client_id": "cid"
   300  		},
   301  		"created_at": ` + referenceTimeStr + `,
   302  		"updated_at": ` + referenceTimeStr + `,
   303  		"scopes": ["s"]
   304  	}`
   305  
   306  	testJSONMarshal(t, u, want)
   307  }
   308  
   309  func TestAuthorization_Marshal(t *testing.T) {
   310  	t.Parallel()
   311  	testJSONMarshal(t, &Authorization{}, "{}")
   312  
   313  	u := &Authorization{
   314  		ID:             Ptr(int64(1)),
   315  		URL:            Ptr("u"),
   316  		Scopes:         []Scope{"s"},
   317  		Token:          Ptr("t"),
   318  		TokenLastEight: Ptr("tle"),
   319  		HashedToken:    Ptr("ht"),
   320  		App: &AuthorizationApp{
   321  			URL:      Ptr("u"),
   322  			Name:     Ptr("n"),
   323  			ClientID: Ptr("cid"),
   324  		},
   325  		Note:        Ptr("n"),
   326  		NoteURL:     Ptr("nu"),
   327  		UpdatedAt:   &Timestamp{referenceTime},
   328  		CreatedAt:   &Timestamp{referenceTime},
   329  		Fingerprint: Ptr("f"),
   330  		User: &User{
   331  			Login:           Ptr("l"),
   332  			ID:              Ptr(int64(1)),
   333  			URL:             Ptr("u"),
   334  			AvatarURL:       Ptr("a"),
   335  			GravatarID:      Ptr("g"),
   336  			Name:            Ptr("n"),
   337  			Company:         Ptr("c"),
   338  			Blog:            Ptr("b"),
   339  			Location:        Ptr("l"),
   340  			Email:           Ptr("e"),
   341  			Hireable:        Ptr(true),
   342  			Bio:             Ptr("b"),
   343  			TwitterUsername: Ptr("t"),
   344  			PublicRepos:     Ptr(1),
   345  			Followers:       Ptr(1),
   346  			Following:       Ptr(1),
   347  			CreatedAt:       &Timestamp{referenceTime},
   348  			SuspendedAt:     &Timestamp{referenceTime},
   349  		},
   350  	}
   351  
   352  	want := `{
   353  		"id": 1,
   354  		"url": "u",
   355  		"scopes": ["s"],
   356  		"token": "t",
   357  		"token_last_eight": "tle",
   358  		"hashed_token": "ht",
   359  		"app": {
   360  			"url": "u",
   361  			"name": "n",
   362  			"client_id": "cid"
   363  		},
   364  		"note": "n",
   365  		"note_url": "nu",
   366  		"updated_at": ` + referenceTimeStr + `,
   367  		"created_at": ` + referenceTimeStr + `,
   368  		"fingerprint": "f",
   369  		"user": {
   370  			"login": "l",
   371  			"id": 1,
   372  			"avatar_url": "a",
   373  			"gravatar_id": "g",
   374  			"name": "n",
   375  			"company": "c",
   376  			"blog": "b",
   377  			"location": "l",
   378  			"email": "e",
   379  			"hireable": true,
   380  			"bio": "b",
   381  			"twitter_username": "t",
   382  			"public_repos": 1,
   383  			"followers": 1,
   384  			"following": 1,
   385  			"created_at": ` + referenceTimeStr + `,
   386  			"suspended_at": ` + referenceTimeStr + `,
   387  			"url": "u"
   388  		}
   389  	}`
   390  
   391  	testJSONMarshal(t, u, want)
   392  }