func TestCreateRequest(t *testing.T) { signer := oauth.GetHMACSigner("asdf", "") p := &Provider{ Secret: "asdf", URL: "http://urltest.com/", ConsumerKey: "12345", Method: "post", } p.Add("resource_link_id", "1086"). Add("context_id", "2"). Add("roles", "Instructor,Administrator"). Add("custom_username", "test"). SetSigner(signer) sig, err := p.Sign() if err != nil { t.Errorf("Error signing %s", err) } if sig == "" { t.Errorf("Request should be signed") } if p.Get("oauth_signature") != sig { t.Errorf("Request should be signed, %s", p.Get("oauth_signature")) log.Printf("%#v", p.values) } if p.Get("oauth_signature_method") != SigHMAC { t.Errorf("Signature method should be HMAC") } u, _ := url.Parse("http://urltest.com/") r := &http.Request{ Method: "POST", URL: u, Body: nil, Form: p.Params(), } pp := NewProvider("asdf", "http://urltest.com/") pp.ConsumerKey = "12345" ok, err := pp.IsValid(r) if err != nil { t.Errorf("Error parsing request %s", err) } if ok != true { t.Errorf("Request should be valid") } }
func TestSign(t *testing.T) { vals := GenerateForm() res, err := getBaseString("post", "http://www.imsglobal.org/developers/LTI/test/v1p1/tool.php", vals) if err != nil { log.Printf("Error generating base string for oauth") } if len(res) != len(OT) { t.Errorf("Wrong lengths %d, %d", len(res), len(OT)) } for i := range res { if res[i] != OT[i] { t.Errorf("%s, %s", res[i:(i+15)], OT[i:(i+15)]) break } } if res != OT { t.Errorf("%s\n%s", OT, res) } signer := oauth.GetHMACSigner("secret", "") signed, err := Sign(vals, "http://www.imsglobal.org/developers/LTI/test/v1p1/tool.php", "post", signer) if err != nil { t.Errorf("Error sigining request %s", err) } if signed != "QWgJfKpJNDrpncgO9oXxJb8vHiE=" { t.Errorf("Provided %s", signed) } }