Пример #1
0
// Check if the user is a Facebook Employee. This only available by
// special permission granted to an application by Facebook.
func (c *Checker) Check(id uint64) bool {
	if is, ok := c.Cache.Get(cacheKey(id)); ok {
		return is.(bool)
	}

	values, err := fbapi.ParamValues(c.App, fields)
	if err != nil {
		c.Logger.Printf("Ignoring error in IsEmployee ParamValues: %s", err)
		return false
	}

	var user user
	u := url.URL{
		Path:     strconv.FormatUint(id, 10),
		RawQuery: values.Encode(),
	}
	req := http.Request{Method: "GET", URL: &u}
	_, err = c.FbApiClient.Do(&req, &user)
	if err != nil {
		if apiErr, ok := err.(*fbapi.Error); ok {
			if apiErr.Code == 100 { // common error with test users
				return false
			}
		}
		c.Logger.Printf("Ignoring error in IsEmployee FbApiClient.Do: %s", err)
		return false
	}

	c.Cache.Add(cacheKey(id), user.IsEmployee)
	return user.IsEmployee
}
Пример #2
0
func TestParamsError(t *testing.T) {
	_, err := fbapi.ParamValues(paramWithError{})
	if err == nil {
		t.Fatal("was expecting error")
	}
	if err.Error() != paramWithErrorMessage {
		t.Fatalf("expected %s got %s", paramWithErrorMessage, err)
	}
}
Пример #3
0
func TestParams(t *testing.T) {
	cases := []struct {
		Params   []fbapi.Param
		Expected url.Values
	}{
		{
			Params:   []fbapi.Param{fbapi.ParamLimit(42)},
			Expected: url.Values{"limit": []string{"42"}},
		},
		{
			Params:   []fbapi.Param{fbapi.ParamOffset(42)},
			Expected: url.Values{"offset": []string{"42"}},
		},
		{
			Params:   []fbapi.Param{fbapi.ParamFields("abc", "def")},
			Expected: url.Values{"fields": []string{"abc,def"}},
		},
		{
			Params:   []fbapi.Param{fbapi.ParamAccessToken("42")},
			Expected: url.Values{"access_token": []string{"42"}},
		},
		{
			Params:   []fbapi.Param{fbapi.ParamDateFormat("42")},
			Expected: url.Values{"date_format": []string{"42"}},
		},
	}

	for _, c := range cases {
		v, err := fbapi.ParamValues(c.Params...)
		if err != nil {
			t.Errorf("case %+v got error %s", c, err)
		}
		if !reflect.DeepEqual(c.Expected, v) {
			t.Fatalf("case\n%+v\nactual:\n%+v", c, v)
		}
	}
}