forked from Mirrors/oauth2
Add testing for ExchangeToken()
This commit is contained in:
parent
58b935b4ea
commit
b838bbaf4d
|
@ -1,11 +1,12 @@
|
||||||
package externalaccount
|
package externalaccount
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/google/go-cmp/cmp"
|
"github.com/google/go-cmp"
|
||||||
"golang.org/x/oauth2"
|
"golang.org/x/oauth2"
|
||||||
"net/http"
|
"io/ioutil"
|
||||||
"net/http/httptest"
|
"net/http"
|
||||||
"testing"
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
var auth = ClientAuthentication{
|
var auth = ClientAuthentication{
|
||||||
|
@ -24,80 +25,56 @@ var tokenRequest = STSTokenExchangeRequest{
|
||||||
Audience: "32555940559.apps.googleusercontent.com", //TODO: Make sure audience is correct in this test (might be mismatched)
|
Audience: "32555940559.apps.googleusercontent.com", //TODO: Make sure audience is correct in this test (might be mismatched)
|
||||||
Scope: []string{"https://www.googleapis.com/auth/devstorage.full_control"},
|
Scope: []string{"https://www.googleapis.com/auth/devstorage.full_control"},
|
||||||
RequestedTokenType: "urn:ietf:params:oauth:token-type:access_token",
|
RequestedTokenType: "urn:ietf:params:oauth:token-type:access_token",
|
||||||
SubjectToken: "eyJhbGciOiJSUzI1NiIsImtpZCI6IjJjNmZhNmY1OTUwYTdjZTQ2NWZjZjI0N2FhMGIwOTQ4MjhhYzk1MmMiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJodHRwczovL2FjY291bnRzLmdvb2dsZS5jb20iLCJhenAiOiIzMjU1NTk0MDU1OS5hcHBzLmdvb2dsZXVzZXJjb250ZW50LmNvbSIsImF1ZCI6IjMyNTU1OTQwNTU5LmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwic3ViIjoiMTEzMzE4NTQxMDA5MDU3Mzc4MzI4IiwiaGQiOiJnb29nbGUuY29tIiwiZW1haWwiOiJpdGh1cmllbEBnb29nbGUuY29tIiwiZW1haWxfdmVy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zjCpf_2ng03IIGbFsTvAUx4wvWhnFO-z4xl2tb13OMCxpkw52dO1ZcFhw0d_1iUj_q0UL9E15ADL4SOr-BVtXerWPhNVBplTw8gzL4HHmo2GGUA_ilQpJzD528BKLygemqy1taXZwOGJEAUYkcKm8DhA0NJWneUyqHN6qbs0wm_d_nZsiFx9CIDblt1dUkgfuPIsno-xrkkkwubcv1WlgA",
|
||||||
SubjectTokenType: "urn:ietf:params:oauth:token-type:jwt",
|
SubjectTokenType: "urn:ietf:params:oauth:token-type:jwt",
|
||||||
}
|
}
|
||||||
|
|
||||||
var serverReq = http.Request{
|
var requestbody = "audience=32555940559.apps.googleusercontent.com&grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Atoken-exchange&requested_token_type=urn%3Aietf%3Aparams%3Aoauth%3Atoken-type%3Aaccess_token&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdevstorage.full_control&subject_token=eyJhbGciOiJSUzI1NiIsImtpIjJjNmZhNmY1OTUwYTdjZTQ2NWZjZjI0N2FhMGIwOTQ4MjhhYzk1MmMiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJodHRwczovL2FjY291bnRzLmdvb2dsZS5jb20iLCJhenAiOiIzMjU1NTk0MDU1OS5hcHBzLmdvb2dsZXVzZXJjb250ZW50LmNvbSIsImF1IjMyNTU1OTQwNTU5LmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwic3ViIjoiMTEzMzE4NTQxMDA5MDU3Mzc4MzI4IiwiaGQiOiJnb29nbGUuY29tIiwiZW1haWwiOiJpdGh1cmllbEBnb29nbGUuY29tIiwiZW1haWxfdmVyaWZpZWQiOnRydWUsImF0X2hhc2giOiJyWUtBTjZwX21rS0U4U2ItN3ZGalBBIiwiaWF0IjoxNjAxNTk0NDY1LCJleHAiOjE2MDE1OTgwNjV9.mWOLjD6ghfgrFNcm_1h-wrpLlKFc2WSS13lu2L5t4549uYhX5DEbI7MmeUEwXSffrns1ljcdbJm4nXymXK3AH6ftRV17O3BnOsWngxKj5eKhzOMF308YNXjBKTDiu_crzjCpf_2ng03IIGbFsTvAUx4wvWhnFO-z4xl2tb13OMCxpkw52dO1ZcFhw0d_1iUj_q0UL9E15ADL4SOr-BVtXerWPhNVBplTw8gzL4HHmo2GGUA_ilQpJzD528BKLygemqy1taXZwOGJEAUYkcKm8DhA0NJWneUyqHN6qbs0wm_d_nZsiFx9CIDblt1dUkgfuPIsno-xrkkkwubcv1WlgA&subject_token_type=urn%3Aietf%3Aparams%3Aoauth%3Atoken-type%3Ajwt"
|
||||||
Method: "POSTURL:/",
|
var responseBody = `{"access_token":"ya29.a0AfH6SMCqKiKaHEpqs9sl6cocVlqG2dvP2g7eURqGNCfUyKZ6lYSHz531aioS3_0w_xdNDfj6A-Hzk6-0-M6olf3O-zfcGFd678lVuvplpclaK4XQ4ete6_9xSUFU08RefwE53rf2OW8FOQvjpQB-PmPhHZTzK99YYHNtjas","issued_token_type":"urn:ietf:params:oauth:token-type:access_token","token_type":"Bearer","expires_in":3600,"scope":"https://www.googleapis.com/auth/cloud-platform"}`
|
||||||
URL: nil,
|
var expectedToken = STSTokenExchangeResponse{
|
||||||
Proto: "HTTP/1.1",
|
AccessToken: "ya29.a0AfH6SMCqKiKaHEpqs9sl6cocVlqG2dvP2g7eURqGNCfUyKZ6lYSHz531aioS3_0w_xdNDfj6A-Hzk6-0-M6olf3O-zfcGFd678lVuvplpclaK4XQ4ete6_9xSUFU08RefwE53rf2OW8FOQvjpQB-PmPhHZTzK99YYHNtjas",
|
||||||
ProtoMajor: 1,
|
IssuedTokenType: "urn:ietf:params:oauth:token-type:access_token",
|
||||||
ProtoMinor: 1,
|
TokenType: "Bearer",
|
||||||
Header: map[string][]string{
|
ExpiresIn: 3600,
|
||||||
"Accept-Encoding": []string{"gzip"},
|
Scope: "https://www.googleapis.com/auth/cloud-platform",
|
||||||
"Authorization": []string{"Basic cmJyZ25vZ25yaG9uZ28zYmk0Z2I5Z2hnOWc6bm90c29zZWNyZXQ="},
|
RefreshToken: "",
|
||||||
"Content-Length": []string{"1192"},
|
|
||||||
"Content-Type": []string{"application/x-www-form-urlencoded"},
|
|
||||||
"User-Agent": []string{"Go-http-client/1.1"},
|
|
||||||
},
|
|
||||||
Body: nil, //TODO: Construct this struct
|
|
||||||
ContentLength: 1192,
|
|
||||||
Close: false,
|
|
||||||
Host: "127.0.0.1:41147", //TODO: Does this conflict due to separate addresses?
|
|
||||||
Form: nil, //TODO: Should Form, PostForm, TransferEncoding, etc be initialized with Make?
|
|
||||||
PostForm: nil,
|
|
||||||
MultipartForm: nil,
|
|
||||||
Trailer: nil,
|
|
||||||
RemoteAddr: "127.0.0.1:52760",
|
|
||||||
RequestURI: "/",
|
|
||||||
TLS: nil,
|
|
||||||
Cancel: nil,
|
|
||||||
Response: nil,
|
|
||||||
}
|
|
||||||
|
|
||||||
var serverResp = http.Response{
|
|
||||||
Status: "200 OK",
|
|
||||||
StatusCode: 200,
|
|
||||||
Proto: "HTTP/1.1",
|
|
||||||
ProtoMajor: 1,
|
|
||||||
ProtoMinor: 1,
|
|
||||||
Header: map[string][]string{
|
|
||||||
"Connection":[]string{"keep-alive"},
|
|
||||||
"Content-Length":[]string{"362"},
|
|
||||||
"Content-Type":[]string{"application/json; charset=utf-8"},
|
|
||||||
"Date":[]string{"Wed, 07 Oct 2020 21:54:27 GMT"},
|
|
||||||
"X-Powered-By":[]string{"Express"},
|
|
||||||
},
|
|
||||||
Body: nil,
|
|
||||||
ContentLength: 0,
|
|
||||||
TransferEncoding: nil,
|
|
||||||
Close: false,
|
|
||||||
Uncompressed: false,
|
|
||||||
Trailer: nil,
|
|
||||||
Request: nil,
|
|
||||||
TLS: nil,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestExchangeToken(t *testing.T) {
|
func TestExchangeToken(t *testing.T) {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
if diff := cmp.Diff(*r, serverReq); diff != "" {
|
if r.URL.String() != "/" {
|
||||||
t.Errorf("mismatched messages received by mock server (-want +got): \n%s", diff)
|
t.Errorf("Unexpected request URL, %v is found.", r.URL)
|
||||||
}
|
}
|
||||||
if r.URL.String() !=
|
headerAuth := r.Header.Get("Authorization")
|
||||||
|
if headerAuth != "Basic cmJyZ25vZ25yaG9uZ28zYmk0Z2I5Z2hnOWc6bm90c29zZWNyZXQ=" {
|
||||||
return
|
t.Errorf("Unexpected autohrization header, %v is found.", headerAuth)
|
||||||
|
}
|
||||||
|
headerContentType := r.Header.Get("Content-Type")
|
||||||
|
if headerContentType != "application/x-www-form-urlencoded]" {
|
||||||
|
t.Errorf("Unexpected Content-Type header, %v is found.", headerContentType)
|
||||||
|
}
|
||||||
|
body, err := ioutil.ReadAll(r.Body)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Failed reading request body: %s.", err)
|
||||||
|
}
|
||||||
|
if string(body) != requestbody {
|
||||||
|
t.Errorf("Unexpected exchange payload, %v is found.", string(body))
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.Write([]byte(responseBody))
|
||||||
}))
|
}))
|
||||||
|
|
||||||
headers := make(map[string][]string)
|
headers := make(map[string][]string)
|
||||||
headers["Content-Type"] = []string{"application/x-www-form-urlencoded"}
|
headers["Content-Type"] = []string{"application/x-www-form-urlencoded"}
|
||||||
|
|
||||||
//TODO: Call TokenExchange, make sure I get the right results
|
|
||||||
resp, err := ExchangeToken(ts.URL, &tokenRequest, auth, headers, nil)
|
resp, err := ExchangeToken(ts.URL, &tokenRequest, auth, headers, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("ExchangeToken failed with error: %s", err)
|
t.Errorf("ExchangeToken failed with error: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if diff := cmp.Diff(resp, expectedToken); diff != "" {
|
||||||
|
t.Errorf("mismatched messages received by mock server (-want +got): \n%v", diff)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue