func TestPasswordStorageErr(t *testing.T) { s := testNull(t) // // Prepare handler // hserv := httptest.NewServer(s.PasswordGrantHandler()) defer hserv.Close() // // REST request // scopes := []string{"enterprise", "intrepid"} scopeStr := strings.Join(scopes, " ") username := "******" password := "******" u := url.UserPassword(username, password) preq := PasswordRequest{ GrantType: "password", Username: "******", Password: password, Scope: scopeStr, } rr := restclient.RequestResponse{ Url: hserv.URL, Method: "POST", Userinfo: u, Data: &preq, } c := restclient.New() c.UnsafeBasicAuth = true status, err := c.Do(&rr) if err != nil { t.Error(err) } assert.Equal(t, 500, status) }
func TestPasswordInvalidScope(t *testing.T) { s := testNull(t) // // Prepare handler // hserv := httptest.NewServer(s.PasswordGrantHandler()) defer hserv.Close() // // REST request // username := "******" password := "******" u := url.UserPassword(username, password) preq := PasswordRequest{ GrantType: "password", Username: "******", Password: password, Scope: "foobar", // invalid scope } rr := restclient.RequestResponse{ Url: hserv.URL, Method: "POST", Userinfo: u, Data: &preq, } c := restclient.New() c.UnsafeBasicAuth = true status, err := c.Do(&rr) if err != nil { t.Error(err) } assert.Equal(t, 400, status) }
func TestPasswordAuthenticateErr(t *testing.T) { a := func(user, password string) (bool, error) { return false, ErrNotImplemented } s := NewProvider(&nullStorage{}, a, GrantAll) // // Prepare handler // hserv := httptest.NewServer(s.PasswordGrantHandler()) defer hserv.Close() // // REST request // username := "******" password := "******" u := url.UserPassword(username, password) rr := restclient.RequestResponse{ Url: hserv.URL, Method: "POST", Userinfo: u, } c := restclient.New() c.UnsafeBasicAuth = true status, err := c.Do(&rr) if err != nil { t.Error(err) } assert.Equal(t, 500, status) }
func TestPasswordNoData(t *testing.T) { s := testNull(t) // // Prepare handler // hserv := httptest.NewServer(s.PasswordGrantHandler()) defer hserv.Close() // // REST request // username := "******" password := "******" u := url.UserPassword(username, password) rr := restclient.RequestResponse{ Url: hserv.URL, Method: "POST", Userinfo: u, } c := restclient.New() c.UnsafeBasicAuth = true status, err := c.Do(&rr) if err != nil { t.Fatal(err) } assert.Equal(t, 400, status) }
// Connect establishes a connection to the Neo4j server. func Connect(uri string) (*Database, error) { var e NeoError db := &Database{ Rc: restclient.New(), } _, err := url.Parse(uri) // Sanity check if err != nil { return nil, err } db.Url = uri req := restclient.RequestResponse{ Url: db.Url, Method: "GET", Result: &db, Error: &e, } status, err := db.Rc.Do(&req) if err != nil { return nil, err } if status != 200 || db.Version == "" { logPretty(req.RawText) log.Println("Status " + strconv.Itoa(status) + " trying to connect to " + uri) return nil, InvalidDatabase } return db, nil }
func NewHeroku(apiKey string) *Heroku { h := Heroku{ ApiKey: apiKey, ApiHref: HerokuApi, rc: restclient.New(), } return &h }
// New returns freshly squeezed Postmaster object with all dependants initialized. func New(key string) *Postmaster { client := restclient.New() userinfo := url.UserPassword(key, "") header := http.Header{ "Content-Type": []string{"application/json"}, "User-Agent": []string{fmt.Sprintf("Postmaster/%.1f Go", VERSION)}, } return &Postmaster{ apiKey: key, client: client, userinfo: userinfo, headers: &header, } }
func (db *DataBase) GetTasksList() *models.TaskList { var result cypherResponse var nerr interface{} query := cypherRequest{ Query: "START r=node(0) MATCH r-[:TASKS]->t<-[:IS_TASK]-task RETURN task", Params: queryParams{}, } url := db.url.String() + "/cypher" r := restclient.RestRequest{ Url: url, Method: restclient.POST, Data: &query, Result: &result, Error: &nerr, } client := restclient.New() _, err := client.Do(&r) if err != nil { panic(err) } tasks := make(models.TaskList, 0) responseData := result.Data for _, row := range responseData { nodeInfo := row[0] self := nodeInfo.HrefSelf selfM := strings.Split(self, "/") id, err := strconv.Atoi(selfM[len(selfM)-1]) if err != nil { panic(err) } task, err := db.GetTask(id, false) if err != nil { panic(errors.New(fmt.Sprintf("Could not get task (id: %s) (%s)", id, err))) } tasks = append(tasks, task) } return &tasks }
// Connect establishes a connection to the Neo4j server. func Connect(uri string) (*Database, error) { var sr serviceRoot var e neoError db := &Database{ rc: restclient.New(), } u, err := url.Parse(uri) if err != nil { return db, err } db.url = u req := restclient.RequestResponse{ Url: u.String(), Method: "GET", Result: &sr, Error: &e, } status, err := db.rc.Do(&req) if err != nil { logPretty(req) return db, err } switch { case status == 404: return db, InvalidDatabase case status != 200 || sr.Version == "": log.Println("Status " + strconv.Itoa(status) + " trying to cconnect to " + u.String()) logPretty(req) return db, BadResponse } // Populate Database struct db.HrefNode = sr.HrefNode db.HrefRefNode = sr.HrefRefNode db.HrefNodeIndex = sr.HrefNodeIndex db.HrefRelIndex = sr.HrefRelIndex db.HrefExtInfo = sr.HrefExtInfo db.HrefRelTypes = sr.HrefRelTypes db.HrefBatch = sr.HrefBatch db.HrefCypher = sr.HrefCypher db.Version = sr.Version // Success! return db, nil }
func doTestPasswordRequest(p *Provider, t *testing.T) { // // Prepare handler // hserv := httptest.NewServer(p.PasswordGrantHandler()) defer hserv.Close() // // REST request // scopes := []string{"enterprise", "intrepid"} scopeStr := strings.Join(scopes, " ") username := "******" password := "******" u := url.UserPassword(username, password) preq := PasswordRequest{ GrantType: "password", Username: "******", Password: password, Scope: scopeStr, Note: "foo bar baz", } var res TokenResponse var e interface{} rr := restclient.RequestResponse{ Url: hserv.URL, Method: "POST", Userinfo: u, Data: &preq, Result: &res, Error: &e, } c := restclient.New() c.UnsafeBasicAuth = true status, err := c.Do(&rr) if err != nil { t.Error(err) } assert.Equal(t, 200, status) assert.NotEqual(t, nil, uuid.Parse(res.AccessToken)) assert.Equal(t, scopeStr, res.Scope) assert.Equal(t, "bearer", res.TokenType) }
func TestPasswordUnauthorizedScope(t *testing.T) { g := func(user, scope string, c *Client) (bool, error) { return false, nil } s := NewProvider(&nullStorage{}, kirkAuthenticator, g) s.Scopes = testScopesAll s.DefaultScopes = testScopesDefault // // Prepare handler // hserv := httptest.NewServer(s.PasswordGrantHandler()) defer hserv.Close() // // REST request // username := "******" password := "******" scopes := []string{"enterprise", "intrepid"} scopeStr := strings.Join(scopes, " ") u := url.UserPassword(username, password) preq := PasswordRequest{ GrantType: "password", Username: "******", Password: password, Scope: scopeStr, } rr := restclient.RequestResponse{ Url: hserv.URL, Method: "POST", Userinfo: u, Data: &preq, } c := restclient.New() c.UnsafeBasicAuth = true status, err := c.Do(&rr) if err != nil { t.Error(err) } assert.Equal(t, 401, status) }
func TestPasswordBadCreds(t *testing.T) { s := testNull(t) // // Prepare handler // hserv := httptest.NewServer(s.PasswordGrantHandler()) defer hserv.Close() // // REST request // username := "******" password := "******" u := url.UserPassword(username, password) preq := PasswordRequest{ GrantType: "password", Username: "******", Password: password, } var res interface{} var e interface{} rr := restclient.RequestResponse{ Url: hserv.URL, Method: "POST", Userinfo: u, Data: &preq, Result: &res, Error: &e, } c := restclient.New() c.UnsafeBasicAuth = true status, err := c.Do(&rr) if err != nil { t.Fatal(err) } assert.Equal(t, 401, status) }