...

Source file src/github.com/google/go-github/v45/github/authorizations_test.go

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

     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  	client, mux, _, teardown := setup()
    19  	defer teardown()
    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: 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  	client, mux, _, teardown := setup()
    56  	defer teardown()
    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: 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  	client, mux, _, teardown := setup()
    93  	defer teardown()
    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  	client, mux, _, teardown := setup()
   121  	defer teardown()
   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  	client, mux, _, teardown := setup()
   148  	defer teardown()
   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: 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  	client, mux, _, teardown := setup()
   184  	defer teardown()
   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  	testJSONMarshal(t, &AuthorizationUpdateRequest{}, "{}")
   209  
   210  	u := &AuthorizationUpdateRequest{
   211  		Scopes:       []string{"s"},
   212  		AddScopes:    []string{"a"},
   213  		RemoveScopes: []string{"r"},
   214  		Note:         String("n"),
   215  		NoteURL:      String("nu"),
   216  		Fingerprint:  String("f"),
   217  	}
   218  
   219  	want := `{
   220  		"scopes": ["s"],
   221  		"add_scopes": ["a"],
   222  		"remove_scopes": ["r"],
   223  		"note": "n",
   224  		"note_url": "nu",
   225  		"fingerprint": "f"
   226  	}`
   227  
   228  	testJSONMarshal(t, u, want)
   229  }
   230  
   231  func TestAuthorizationRequest_Marshal(t *testing.T) {
   232  	testJSONMarshal(t, &AuthorizationRequest{}, "{}")
   233  
   234  	u := &AuthorizationRequest{
   235  		Scopes:       []Scope{"s"},
   236  		ClientID:     String("cid"),
   237  		ClientSecret: String("cs"),
   238  		Note:         String("n"),
   239  		NoteURL:      String("nu"),
   240  		Fingerprint:  String("f"),
   241  	}
   242  
   243  	want := `{
   244  		"scopes": ["s"],
   245  		"client_id": "cid",
   246  		"client_secret": "cs",
   247  		"note": "n",
   248  		"note_url": "nu",
   249  		"fingerprint": "f"
   250  	}`
   251  
   252  	testJSONMarshal(t, u, want)
   253  }
   254  
   255  func TestAuthorizationApp_Marshal(t *testing.T) {
   256  	testJSONMarshal(t, &AuthorizationApp{}, "{}")
   257  
   258  	u := &AuthorizationApp{
   259  		URL:      String("u"),
   260  		Name:     String("n"),
   261  		ClientID: String("cid"),
   262  	}
   263  
   264  	want := `{
   265  		"url": "u",
   266  		"name": "n",
   267  		"client_id": "cid"
   268  	}`
   269  
   270  	testJSONMarshal(t, u, want)
   271  }
   272  
   273  func TestGrant_Marshal(t *testing.T) {
   274  	testJSONMarshal(t, &Grant{}, "{}")
   275  
   276  	u := &Grant{
   277  		ID:  Int64(1),
   278  		URL: String("u"),
   279  		App: &AuthorizationApp{
   280  			URL:      String("u"),
   281  			Name:     String("n"),
   282  			ClientID: String("cid"),
   283  		},
   284  		CreatedAt: &Timestamp{referenceTime},
   285  		UpdatedAt: &Timestamp{referenceTime},
   286  		Scopes:    []string{"s"},
   287  	}
   288  
   289  	want := `{
   290  		"id": 1,
   291  		"url": "u",
   292  		"app": {
   293  			"url": "u",
   294  			"name": "n",
   295  			"client_id": "cid"
   296  		},
   297  		"created_at": ` + referenceTimeStr + `,
   298  		"updated_at": ` + referenceTimeStr + `,
   299  		"scopes": ["s"]
   300  	}`
   301  
   302  	testJSONMarshal(t, u, want)
   303  }
   304  
   305  func TestAuthorization_Marshal(t *testing.T) {
   306  	testJSONMarshal(t, &Authorization{}, "{}")
   307  
   308  	u := &Authorization{
   309  		ID:             Int64(1),
   310  		URL:            String("u"),
   311  		Scopes:         []Scope{"s"},
   312  		Token:          String("t"),
   313  		TokenLastEight: String("tle"),
   314  		HashedToken:    String("ht"),
   315  		App: &AuthorizationApp{
   316  			URL:      String("u"),
   317  			Name:     String("n"),
   318  			ClientID: String("cid"),
   319  		},
   320  		Note:        String("n"),
   321  		NoteURL:     String("nu"),
   322  		UpdatedAt:   &Timestamp{referenceTime},
   323  		CreatedAt:   &Timestamp{referenceTime},
   324  		Fingerprint: String("f"),
   325  		User: &User{
   326  			Login:           String("l"),
   327  			ID:              Int64(1),
   328  			URL:             String("u"),
   329  			AvatarURL:       String("a"),
   330  			GravatarID:      String("g"),
   331  			Name:            String("n"),
   332  			Company:         String("c"),
   333  			Blog:            String("b"),
   334  			Location:        String("l"),
   335  			Email:           String("e"),
   336  			Hireable:        Bool(true),
   337  			Bio:             String("b"),
   338  			TwitterUsername: String("t"),
   339  			PublicRepos:     Int(1),
   340  			Followers:       Int(1),
   341  			Following:       Int(1),
   342  			CreatedAt:       &Timestamp{referenceTime},
   343  			SuspendedAt:     &Timestamp{referenceTime},
   344  		},
   345  	}
   346  
   347  	want := `{
   348  		"id": 1,
   349  		"url": "u",
   350  		"scopes": ["s"],
   351  		"token": "t",
   352  		"token_last_eight": "tle",
   353  		"hashed_token": "ht",
   354  		"app": {
   355  			"url": "u",
   356  			"name": "n",
   357  			"client_id": "cid"
   358  		},
   359  		"note": "n",
   360  		"note_url": "nu",
   361  		"updated_at": ` + referenceTimeStr + `,
   362  		"created_at": ` + referenceTimeStr + `,
   363  		"fingerprint": "f",
   364  		"user": {
   365  			"login": "l",
   366  			"id": 1,
   367  			"avatar_url": "a",
   368  			"gravatar_id": "g",
   369  			"name": "n",
   370  			"company": "c",
   371  			"blog": "b",
   372  			"location": "l",
   373  			"email": "e",
   374  			"hireable": true,
   375  			"bio": "b",
   376  			"twitter_username": "t",
   377  			"public_repos": 1,
   378  			"followers": 1,
   379  			"following": 1,
   380  			"created_at": ` + referenceTimeStr + `,
   381  			"suspended_at": ` + referenceTimeStr + `,
   382  			"url": "u"
   383  		}
   384  	}`
   385  
   386  	testJSONMarshal(t, u, want)
   387  }
   388  

View as plain text