示例#1
0
func setUP() nerdz.OAuth2AccessData {
	var at nerdz.OAuth2AccessData
	nerdz.Db().First(&at, uint64(1))

	// since we got db access, we update the created_at field and make the request again
	at.CreatedAt = time.Now()
	if err := nerdz.Db().Updates(&at); err != nil {
		panic(err.Error())
	}
	return at
}
示例#2
0
func cleanUP() {
	var at nerdz.OAuth2AccessData
	nerdz.Db().First(&at, uint64(1))

	// since we got db access, we update the created_at field and make the request again
	at.CreatedAt = time.Now()
	// Make the access token expire again to make next tests
	at.CreatedAt = time.Date(2010, 1, 1, 1, 1, 1, 1, time.UTC)
	if err := nerdz.Db().Updates(&at); err != nil {
		panic(err.Error())
	}
}
示例#3
0
// Notifications is the route for the stream of notifications for the current user
func Notifications() echo.HandlerFunc {
	return func(c echo.Context) error {
		accessData := c.Get("accessData").(*osin.AccessData)
		if accessData == nil {
			return c.String(http.StatusInternalServerError, "Invalid authorization")
		}

		websocket.Handler(func(ws *websocket.Conn) {
			// Listen from notification sent on DB channel u<ID>
			nerdz.Db().Listen("u"+strconv.Itoa(int(accessData.UserData.(uint64))), func(payload ...string) {
				if len(payload) == 1 {
					if websocket.Message.Send(ws, payload[0]) != nil {
						return
					}
				}
			})

			// try to read from client (we dont' expect a message) to prevent websocket closing
			for {
				var m string
				if websocket.Message.Receive(ws, &m) != nil {
					// If here, client closed the connection
					break
				}
			}
		}).ServeHTTP(c.Response(), c.Request())
		return nil
	}
}
示例#4
0
// Test GET on Group /users and /me /projects
// we expect the same responses if :id = current logged user
func TestGETAndOnGroupUsers(t *testing.T) {
	endpoints := []string{"/v1/users/1", "/v1/me"}
	for _, endpoint := range endpoints {
		// Authorize
		// extract stored access_token
		// thus I manually generated and stored an access token for app1
		// this is done here only, in a real world application the user follow the OAuth flows and get the access token
		at := setUP()
		res := GETRequest(endpoint, at.AccessToken)

		// since we got db access, we update the created_at field and make the request again
		at.CreatedAt = time.Now()
		if err := nerdz.Db().Updates(&at); err != nil {
			t.Fatal(err.Error())
		}

		res = GETRequest(endpoint, at.AccessToken)

		dec := json.NewDecoder(res.Body)

		var mapData igor.JSON

		if err := dec.Decode(&mapData); err != nil {
			t.Fatalf("Unable to decode received data: %+v", err)
		}

		//mapData := make(map[string]interface{})

		res = GETRequest(endpoint+"/friends", at.AccessToken)

		if res.Code != http.StatusOK {
			t.Fatalf("Error in GET request: status code=%d", res.Code)
		}

		dec = json.NewDecoder(res.Body)

		if err := dec.Decode(&mapData); err != nil {
			t.Fatalf("Unable to decode received data: %+v", err)
		}

		// User 1 has 3 friends
		if lenData := len(mapData["data"].([]interface{})); lenData != 3 {
			t.Fatalf("Incorrect retrived friends. User(1) has 3 friends, got %d", lenData)
		}

		res = GETRequest(endpoint+"/following/users", at.AccessToken)
		if res.Code != http.StatusOK {
			t.Fatalf("Error in GET request: status code=%d", res.Code)
		}
		dec = json.NewDecoder(res.Body)
		if err := dec.Decode(&mapData); err != nil {
			t.Fatalf("Unable to decode received data: %+v", err)
		}
		// User 1 has 4 followers
		if lenData := len(mapData["data"].([]interface{})); lenData != 4 {
			t.Fatalf("Incorrect retrived following. User(1) has 5 followers, got %d", lenData)
		}

		// User 1 has 5 followers and 4 following
		res = GETRequest(endpoint+"/followers", at.AccessToken)
		if res.Code != http.StatusOK {
			t.Fatalf("Error in GET request: status code=%d", res.Code)
		}
		dec = json.NewDecoder(res.Body)
		if err := dec.Decode(&mapData); err != nil {
			t.Fatalf("Unable to decode received data: %+v", err)
		}
		// User 1 has 5 followers
		if lenData := len(mapData["data"].([]interface{})); lenData != 5 {
			t.Fatalf("Incorrect retrived followers. User(1) has 5 followers, got %d", lenData)
		}

		res = GETRequest(endpoint+"/posts", at.AccessToken)

		if res.Code != http.StatusOK {
			t.Fatalf("Error in GET request: status code=%d", res.Code)
		}

		dec = json.NewDecoder(res.Body)

		if err := dec.Decode(&mapData); err != nil {
			t.Fatalf("Unable to decode received data: %+v", err)
		}

		if len(mapData["data"].([]interface{})) != 20 {
			t.Fatalf("Expected 20 posts, but got: %d\n", len(mapData["data"].([]interface{})))
		}

		res = GETRequest(endpoint+"/posts?n=10", at.AccessToken)

		if res.Code != http.StatusOK {
			t.Fatalf("Error in GET request: status code=%d", res.Code)
		}

		dec = json.NewDecoder(res.Body)

		if err := dec.Decode(&mapData); err != nil {
			t.Fatalf("Unable to decode received data: %+v", err)
		}

		if lenData := len(mapData["data"].([]interface{})); lenData != 10 {
			t.Fatalf("Unable to retrieve correctly posts: lenData=%d != 10", lenData)
		}

		res = GETRequest(endpoint+"/posts/6", at.AccessToken)
		if res.Code != http.StatusOK {
			t.Fatalf("Error in GET request: status code=%d, body: %s", res.Code, res.Body)
		}

		dec = json.NewDecoder(res.Body)

		if err := dec.Decode(&mapData); err != nil {
			t.Fatalf("Unable to decode received data: %+v", err)
		}

		if !strings.Contains(mapData["data"].(map[string]interface{})["message"].(string), "PROGETTO") {
			t.Fatalf("expected the admin.6 post, but got: %v", mapData["data"])
		}

		// admin.20 has 3 comments
		res = GETRequest(endpoint+"/posts/20/comments", at.AccessToken)

		if res.Code != http.StatusOK {
			t.Fatalf("Error in GET request: status code=%d", res.Code)
		}

		dec = json.NewDecoder(res.Body)

		if err := dec.Decode(&mapData); err != nil {
			t.Fatalf("Unable to decode received data: %+v", err)
		}

		if lenData := len(mapData["data"].([]interface{})); lenData != 3 {
			t.Fatalf("Incorrect number of comments in GET "+endpoint+"/posts/20/comments. Expected 3 got %d", lenData)
		}

		res = GETRequest(endpoint+"/posts/20/comments?n=1&fields=message", at.AccessToken)

		if res.Code != http.StatusOK {
			t.Fatalf("Error in GET request: status code=%d", res.Code)
		}

		dec = json.NewDecoder(res.Body)

		if err := dec.Decode(&mapData); err != nil {
			t.Fatalf("Unable to decode received data: %+v", err)
		}

		if lenData := len(mapData["data"].([]interface{})); lenData != 1 {
			t.Fatalf("Incorrect number of comments in GET "+endpoint+"/posts/20/comments?n=1&fields=message. Expected 1 got %d", lenData)
		}

		// test single comment based on comment id (hcid), extract hcid and message only
		res = GETRequest(endpoint+"/posts/20/comments/224?fields=message,hcid", at.AccessToken)
		dec = json.NewDecoder(res.Body)
		if err := dec.Decode(&mapData); err != nil {
			t.Fatalf("Unable to decode received data: %+v", err)
		}
		if lenData := len(mapData["data"].(map[string]interface{})); lenData != 2 {
			t.Fatalf("Incorrect number of comments in GET "+endpoint+"/posts/20/comments/224?fields=message,hcid. Expected 1 got %d", lenData)
		}

		data := mapData["data"].(map[string]interface{})
		if !strings.Contains(data["message"].(string), "VEDERE GENTE") {
			t.Fatalf("Expected a message that contains VEDERE GENTE, but got %s\n", data["message"].(string))
		}
		cleanUP()
	}
}