Skip to content

Commit

Permalink
* Access token tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RangelReale committed Jun 8, 2014
1 parent 234d4ab commit 1c4a8f5
Show file tree
Hide file tree
Showing 3 changed files with 221 additions and 2 deletions.
3 changes: 3 additions & 0 deletions access.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package osin

import (
"errors"
"net/http"
"time"
)
Expand Down Expand Up @@ -95,10 +96,12 @@ func (s *Server) HandleAccessRequest(w *Response, r *http.Request) *AccessReques
if r.Method == "GET" {
if !s.Config.AllowGetAccessRequest {
w.SetError(E_INVALID_REQUEST, "")
w.InternalError = errors.New("Request must be POST")
return nil
}
} else if r.Method != "POST" {
w.SetError(E_INVALID_REQUEST, "")
w.InternalError = errors.New("Request must be POST")
return nil
}

Expand Down
195 changes: 195 additions & 0 deletions access_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
package osin

import (
"net/http"
"net/url"
"testing"
)

func TestAccessAuthorizationCode(t *testing.T) {
sconfig := NewServerConfig()
sconfig.AllowedAccessTypes = AllowedAccessType{AUTHORIZATION_CODE}
server := NewServer(sconfig, NewTestingStorage())
server.AccessTokenGen = &TestingAccessTokenGen{}
resp := server.NewResponse()

req, err := http.NewRequest("POST", "http://localhost:14000/appauth", nil)
if err != nil {
t.Fatal(err)
}
req.SetBasicAuth("1234", "aabbccdd")

req.Form = make(url.Values)
req.Form.Set("grant_type", string(AUTHORIZATION_CODE))
req.Form.Set("code", "9999")
req.Form.Set("state", "a")
req.PostForm = make(url.Values)

if ar := server.HandleAccessRequest(resp, req); ar != nil {
ar.Authorized = true
server.FinishAccessRequest(resp, req, ar)
}

//fmt.Printf("%+v", resp)

if resp.IsError && resp.InternalError != nil {
t.Fatalf("Error in response: %s", resp.InternalError)
}

if resp.IsError {
t.Fatalf("Should not be an error")
}

if resp.Type != DATA {
t.Fatalf("Response should be data")
}

if d := resp.Output["access_token"]; d != "1" {
t.Fatalf("Unexpected access token: %s", d)
}

if d := resp.Output["refresh_token"]; d != "r1" {
t.Fatalf("Unexpected refresh token: %s", d)
}
}

func TestAccessRefreshToken(t *testing.T) {
sconfig := NewServerConfig()
sconfig.AllowedAccessTypes = AllowedAccessType{REFRESH_TOKEN}
server := NewServer(sconfig, NewTestingStorage())
server.AccessTokenGen = &TestingAccessTokenGen{}
resp := server.NewResponse()

req, err := http.NewRequest("POST", "http://localhost:14000/appauth", nil)
if err != nil {
t.Fatal(err)
}
req.SetBasicAuth("1234", "aabbccdd")

req.Form = make(url.Values)
req.Form.Set("grant_type", string(REFRESH_TOKEN))
req.Form.Set("refresh_token", "r9999")
req.Form.Set("state", "a")
req.PostForm = make(url.Values)

if ar := server.HandleAccessRequest(resp, req); ar != nil {
ar.Authorized = true
server.FinishAccessRequest(resp, req, ar)
}

//fmt.Printf("%+v", resp)

if resp.IsError && resp.InternalError != nil {
t.Fatalf("Error in response: %s", resp.InternalError)
}

if resp.IsError {
t.Fatalf("Should not be an error")
}

if resp.Type != DATA {
t.Fatalf("Response should be data")
}

if d := resp.Output["access_token"]; d != "1" {
t.Fatalf("Unexpected access token: %s", d)
}

if d := resp.Output["refresh_token"]; d != "r1" {
t.Fatalf("Unexpected refresh token: %s", d)
}
}

func TestAccessPassword(t *testing.T) {
sconfig := NewServerConfig()
sconfig.AllowedAccessTypes = AllowedAccessType{PASSWORD}
server := NewServer(sconfig, NewTestingStorage())
server.AccessTokenGen = &TestingAccessTokenGen{}
resp := server.NewResponse()

req, err := http.NewRequest("POST", "http://localhost:14000/appauth", nil)
if err != nil {
t.Fatal(err)
}
req.SetBasicAuth("1234", "aabbccdd")

req.Form = make(url.Values)
req.Form.Set("grant_type", string(PASSWORD))
req.Form.Set("username", "testing")
req.Form.Set("password", "testing")
req.Form.Set("state", "a")
req.PostForm = make(url.Values)

if ar := server.HandleAccessRequest(resp, req); ar != nil {
ar.Authorized = ar.Username == "testing" && ar.Password == "testing"
server.FinishAccessRequest(resp, req, ar)
}

//fmt.Printf("%+v", resp)

if resp.IsError && resp.InternalError != nil {
t.Fatalf("Error in response: %s", resp.InternalError)
}

if resp.IsError {
t.Fatalf("Should not be an error")
}

if resp.Type != DATA {
t.Fatalf("Response should be data")
}

if d := resp.Output["access_token"]; d != "1" {
t.Fatalf("Unexpected access token: %s", d)
}

if d := resp.Output["refresh_token"]; d != "r1" {
t.Fatalf("Unexpected refresh token: %s", d)
}
}

func TestAccessClientCredentials(t *testing.T) {
sconfig := NewServerConfig()
sconfig.AllowedAccessTypes = AllowedAccessType{CLIENT_CREDENTIALS}
server := NewServer(sconfig, NewTestingStorage())
server.AccessTokenGen = &TestingAccessTokenGen{}
resp := server.NewResponse()

req, err := http.NewRequest("POST", "http://localhost:14000/appauth", nil)
if err != nil {
t.Fatal(err)
}
req.SetBasicAuth("1234", "aabbccdd")

req.Form = make(url.Values)
req.Form.Set("grant_type", string(CLIENT_CREDENTIALS))
req.Form.Set("state", "a")
req.PostForm = make(url.Values)

if ar := server.HandleAccessRequest(resp, req); ar != nil {
ar.Authorized = true
server.FinishAccessRequest(resp, req, ar)
}

//fmt.Printf("%+v", resp)

if resp.IsError && resp.InternalError != nil {
t.Fatalf("Error in response: %s", resp.InternalError)
}

if resp.IsError {
t.Fatalf("Should not be an error")
}

if resp.Type != DATA {
t.Fatalf("Response should be data")
}

if d := resp.Output["access_token"]; d != "1" {
t.Fatalf("Unexpected access token: %s", d)
}

if d := resp.Output["refresh_token"]; d != "r1" {
t.Fatalf("Unexpected refresh token: %s", d)
}
}
25 changes: 23 additions & 2 deletions storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,34 @@ func NewTestingStorage() *TestingStorage {
RedirectUri: "http://localhost:14000/appauth",
}

r.access["9999"] = &AccessData{
r.authorize["9999"] = &AuthorizeData{
Client: r.clients["1234"],
AccessToken: "9999",
Code: "9999",
ExpiresIn: 3600,
CreatedAt: time.Now(),
RedirectUri: "http://localhost:14000/appauth",
}

r.access["9999"] = &AccessData{
Client: r.clients["1234"],
AuthorizeData: r.authorize["9999"],
AccessToken: "9999",
ExpiresIn: 3600,
CreatedAt: time.Now(),
}

r.access["r9999"] = &AccessData{
Client: r.clients["1234"],
AuthorizeData: r.authorize["9999"],
AccessData: r.access["9999"],
AccessToken: "9999",
RefreshToken: "r9999",
ExpiresIn: 3600,
CreatedAt: time.Now(),
}

r.refresh["r9999"] = "9999"

return r
}

Expand Down

0 comments on commit 1c4a8f5

Please sign in to comment.