forked from writeas/osin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
234d4ab
commit 1c4a8f5
Showing
3 changed files
with
221 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters