github.com/crosbymichael/octokat@v0.0.0-20160826194511-076a32289ed5/authorizations_test.go (about)

     1  package octokat
     2  
     3  import (
     4  	"encoding/json"
     5  	"github.com/bmizerany/assert"
     6  	"net/http"
     7  	"reflect"
     8  	"testing"
     9  )
    10  
    11  func TestAuthorizations(t *testing.T) {
    12  	setup()
    13  	defer tearDown()
    14  
    15  	mux.HandleFunc("/authorizations", func(w http.ResponseWriter, r *http.Request) {
    16  		testMethod(t, r, "GET")
    17  		respondWith(w, loadFixture("authorizations.json"))
    18  	})
    19  
    20  	auths, _ := client.Authorizations(nil)
    21  	assert.Equal(t, 1, len(auths))
    22  
    23  	firstAuth := auths[0]
    24  	assert.Equal(t, 1, firstAuth.ID)
    25  	assert.Equal(t, "https://api.github.com/authorizations/1", firstAuth.URL)
    26  	assert.Equal(t, "456", firstAuth.Token)
    27  	assert.Equal(t, "", firstAuth.Note)
    28  	assert.Equal(t, "", firstAuth.NoteURL)
    29  	assert.Equal(t, "2012-11-16 01:05:51 +0000 UTC", firstAuth.CreatedAt.String())
    30  	assert.Equal(t, "2013-08-21 03:29:51 +0000 UTC", firstAuth.UpdatedAt.String())
    31  
    32  	app := App{ClientID: "123", URL: "http://localhost:8080", Name: "Test"}
    33  	assert.Equal(t, app, firstAuth.App)
    34  
    35  	assert.Equal(t, 2, len(firstAuth.Scopes))
    36  	scopes := []string{"repo", "user"}
    37  	assert.T(t, reflect.DeepEqual(firstAuth.Scopes, scopes))
    38  }
    39  
    40  func TestCreateAuthorization(t *testing.T) {
    41  	setup()
    42  	defer tearDown()
    43  
    44  	params := AuthorizationParams{Scopes: []string{"public_repo"}}
    45  
    46  	mux.HandleFunc("/authorizations", func(w http.ResponseWriter, r *http.Request) {
    47  		var authParams AuthorizationParams
    48  		json.NewDecoder(r.Body).Decode(&authParams)
    49  		assert.T(t, reflect.DeepEqual(authParams, params))
    50  
    51  		testMethod(t, r, "POST")
    52  		respondWith(w, loadFixture("create_authorization.json"))
    53  	})
    54  
    55  	options := Options{Params: params}
    56  	auth, _ := client.CreateAuthorization(&options)
    57  
    58  	assert.Equal(t, 3844190, auth.ID)
    59  	assert.Equal(t, "https://api.github.com/authorizations/3844190", auth.URL)
    60  	assert.Equal(t, "123", auth.Token)
    61  	assert.Equal(t, "", auth.Note)
    62  	assert.Equal(t, "", auth.NoteURL)
    63  	assert.Equal(t, "2013-09-28 18:44:39 +0000 UTC", auth.CreatedAt.String())
    64  	assert.Equal(t, "2013-09-28 18:44:39 +0000 UTC", auth.UpdatedAt.String())
    65  
    66  	app := App{ClientID: "00000000000000000000", URL: "http://developer.github.com/v3/oauth/#oauth-authorizations-api", Name: "GitHub API"}
    67  	assert.Equal(t, app, auth.App)
    68  
    69  	assert.Equal(t, 1, len(auth.Scopes))
    70  	scopes := []string{"public_repo"}
    71  	assert.T(t, reflect.DeepEqual(auth.Scopes, scopes))
    72  }