Beispiel #1
0
 def test_del_like(self):
     """
     Unit Test API for creating and deleting a CheckpointLike (like:dekete/put)
     """
     test_user_with_checkpoint = self.create_saved_test_user_with_checkpoint()
     annoying_user_that_likes_everything = self.create_saved_test_user()
     
     #prep api call
     data = {"user_id": annoying_user_that_likes_everything.user_obj.id,
             "signature": gen_signature("put", 
                                        "like",
                                        gen_api_key(annoying_user_that_likes_everything.user_obj.access_token, 
                                                    annoying_user_that_likes_everything.user_obj.id)),
             "user_checkpoint_id": test_user_with_checkpoint.user_checkpoint_obj.id, 
             }
     
     data2 = {"user_id": annoying_user_that_likes_everything.user_obj.id,
             "signature": gen_signature("delete", 
                                        "like",
                                        gen_api_key(annoying_user_that_likes_everything.user_obj.access_token, 
                                                    annoying_user_that_likes_everything.user_obj.id)),
             "user_checkpoint_id": test_user_with_checkpoint.user_checkpoint_obj.id, 
             }  
     
     resp = self.client.put("/like/", data=data)
     assert "like_id" in resp.data
     
     resp = self.client.delete("/like/?" + urllib.urlencode(data2))
     assert "ok" in resp.data
Beispiel #2
0
def user_login():
    """
    (PUT: user)
    Method to handle when a user authenticates (from Facebook), be it a new user, or recurring user
    """

    fb_code = request.form.get("fb_code", None)
    access_token = request.form.get("access_token", None)
    
    if access_token is None:
        access_token = get_user_access_token(fb_code, APP_ID, APP_SECRET)
    
    fb_user_info, user = save_user(access_token, fb_code)
    api_key = gen_api_key(access_token, user.id)
    
    friends = get_friends(user, exclude_self=True)

    return jsonify({"status": "ok",
                    "result": {
                        "user": {
                                 "id": user.id,
                                 "name": user.facebook_user.name, 
                                 },
                        "friends": sanify_friends(friends),
                        "facebook_user_id": fb_user_info["id"],
                        "facebook_portrait_url": "https://graph.facebook.com/%s/picture" % user.facebook_user.id,
                        "api_key": api_key,
                    }
                    })
    def test_new_invalid_checkpoint(self):
        """
        test the new_checkpoint API, with an invalid checkpoint (without expiry/price)
        """

        # create/save user
        authcode = "someauthcode"
        test_user = self.create_facebook_test_user()
        fb_info, user = save_user(test_user["access_token"], authcode)

        # prep image file
        image_path = join(get_resources_abs_path(), "test/images/timbre.jpg")
        image_file = open(image_path, "r")
        image = base64.encodestring(image_file.read())

        # craft request params
        data = {
            "user_id": user.id,
            "signature": gen_signature("put", "checkpoint", gen_api_key(user.access_token, user.id)),
            "name": random_string(),
            "longitude": 2.0,
            "latitude": 1.0,
            "description": random_string(),
            "image": image,
            "type:": "play",
        }

        # send it
        response = self.client.put("/checkpoint/", data=data)
        assert "Requires at least a price or expiry." in response.data
    def test_get_checkpoint_near(self):
        """
        test the nearby-checkpoints functionality of (get:checkpoint) api 
        """
        # group a
        a_user1 = self.create_saved_test_user()
        a_user2 = self.create_saved_test_user()
        self.befriend_test_user(a_user1, [a_user2])

        # anon users
        anon_user1 = self.create_saved_test_user()

        # create checkpoints in group a
        checkpoint_a_data = CheckpointTests.mock_checkpoint_data(a_user1.user_obj.id, (999.9, 999.9))
        a1_checkpoint = add_checkpoint(*checkpoint_a_data)
        a1_ucp = add_checkpoint_to_user(a_user1.user_obj, a1_checkpoint)

        checkpoint_b_data = CheckpointTests.mock_checkpoint_data(a_user2.user_obj.id, (-999.9, -999.9))
        a2_checkpoint = add_checkpoint(*checkpoint_b_data)
        a2_ucp = add_checkpoint_to_user(a_user2.user_obj, a2_checkpoint)

        checkpoint_c_data = CheckpointTests.mock_checkpoint_data(anon_user1.user_obj.id, (999.9, 999.9))
        a3_checkpoint = add_checkpoint(*checkpoint_c_data)
        a3_ucp = add_checkpoint_to_user(anon_user1.user_obj, a3_checkpoint)

        # build data
        data = {
            "user_id": a_user1.user_obj.id,
            "signature": gen_signature(
                "get", "checkpoint", gen_api_key(a_user1.user_obj.access_token, a_user1.user_obj.id)
            ),
            "type": "near",
            "latitude": 999.9,
            "longitude": 999.9,
            "radius": 0.1,
        }

        response = self.client.get("/checkpoint/?" + urllib.urlencode(data))
        json_response = simplejson.loads(response.data)

        assert json_response["status"] == "ok"

        # in friend_ucp
        assert a1_ucp.checkpoint.name in simplejson.dumps((json_response["friends_checkpoints"]))
        assert not a2_ucp.checkpoint.name in simplejson.dumps((json_response["friends_checkpoints"]))
        assert not a3_ucp.checkpoint.name in simplejson.dumps((json_response["friends_checkpoints"]))

        # in recent activity
        assert a2_ucp.checkpoint.name in simplejson.dumps(json_response["recent_checkpoints"])

        # in anon ucp
        assert a3_ucp.checkpoint.name in simplejson.dumps(json_response["anon_checkpoints"])
        assert not a2_ucp.checkpoint.name in simplejson.dumps(json_response["anon_checkpoints"])
        assert not a1_ucp.checkpoint.name in simplejson.dumps(json_response["anon_checkpoints"])
Beispiel #5
0
 def test_get_friends(self):
     user = self.create_saved_test_user()
     friend_a, friend_b = self.create_saved_test_user(), self.create_saved_test_user()
     self.befriend_test_user(user, [friend_a, friend_b])
     
     data = {"user_id": user.user_obj.id,
             "signature": gen_signature("get", "friends", gen_api_key(user.user_obj.access_token, user.user_obj.id))}
     
     resp = self.client.get("/user/friends/?" + urllib.urlencode(data))
     json_res = simplejson.loads(resp.data)
     assert len(json_res["friends"]) == 2
     
 def test_authorize_verb(self):
     user = self.create_saved_test_user()
     
     auth_code = user.authcode
     user_id = user.user_obj.id
     api_key = gen_api_key(auth_code, user_id)
     
     client_side_signature_a = gen_signature("get", "noun", api_key)
     client_side_signature_b = gen_signature("not_get", "noun", api_key)
     
     assert is_api_key_validated(auth_code, user_id, client_side_signature_a, "get", "noun")
     assert not is_api_key_validated(auth_code, user_id, client_side_signature_b, "get", "noun")
 def test_del_user_checkpoint(self):
     """
     unit test for (delete:user_checkpoint) 
     """
     user = self.create_saved_test_user_with_checkpoint()
     
     data = {"user_id": user.user_obj.id,
             "signature": gen_signature("delete", "user_checkpoint", gen_api_key(user.user_obj.access_token, user.user_obj.id)),
             "checkpoint_id": user.user_checkpoint_obj.checkpoint_id,
             }
     
     response = self.client.delete("/user_checkpoint/?" + urllib.urlencode(data))
     json_res = simplejson.loads(response.data)
     
     assert json_res["status"] == "ok" 
 def test_add_user_checkpoint(self):
     """
     unit test for (put:user_checkpoint)
     """
     user_a = self.create_saved_test_user_with_checkpoint()
     user_b = self.create_saved_test_user()
     
     data = {"user_id": user_b.user_obj.id,
             "signature": gen_signature("put", "user_checkpoint",
                                        gen_api_key(user_b.user_obj.access_token, user_b.user_obj.id)),
             "checkpoint_id": user_a.checkpoint_obj.id,
             }
     
     response = self.client.put("/user_checkpoint/", data=data)
     assert "ok" in response.data
Beispiel #9
0
 def test_get_stats(self):
     user = self.create_saved_test_user_with_checkpoint()
     other_user = self.create_saved_test_user()
     
     add_like(other_user.user_obj, user.user_checkpoint_obj)
     add_share(other_user.user_obj, user.user_obj, user.user_checkpoint_obj)
     
     data = {"user_id": user.user_obj.id,
             "signature": gen_signature("get", "stats", gen_api_key(user.user_obj.access_token, user.user_obj.id))}
     
     resp = self.client.get("/user/stats/?" + urllib.urlencode(data))
     json_res = simplejson.loads(resp.data)
     assert json_res["total_likes"] == 1
     assert json_res["total_reshares"] == 1
     
Beispiel #10
0
    def test_catalog(self):
        """
        tests add_checkpoint_to_catalog()
        """

        user_with_checkpoint = self.create_saved_test_user_with_checkpoint()
        user_for_catalog_add = self.create_saved_test_user()
        
        data = {"user_id": user_for_catalog_add.user_obj.id,
                "signature": gen_signature("post", 
                                           "catalog", 
                                           gen_api_key(user_for_catalog_add.user_obj.access_token, 
                                                       user_for_catalog_add.user_obj.id)),
                "user_checkpoint_id": user_with_checkpoint.user_checkpoint_obj.id, 
                }
        
        response = self.client.post("/catalog/", data=data)
        assert "ok" in response.data
Beispiel #11
0
    def test_get_checkpoint_details(self):
        """
        tests checkpoint detail api
        """
        user_a = self.create_saved_test_user_with_checkpoint()
        user_b = self.create_saved_test_user()
        add_like(user_a.user_obj, user_a.user_checkpoint_obj)

        data = {
            "user_id": user_a.user_obj.id,
            "signature": gen_signature(
                "get", "checkpoint", gen_api_key(user_a.user_obj.access_token, user_a.user_obj.id)
            ),
            "user_checkpoint_id": user_a.user_checkpoint_obj.id,
        }

        response = self.client.get("/checkpoint/?" + urllib.urlencode(data))
        json_res = simplejson.loads(response.data)
        assert json_res["current_user_like"] == True
        assert json_res["status"] == "ok"
Beispiel #12
0
    def test_get_checkpoint_search(self):
        """
        test the search functionality of (get:checkpoint) api 
        """
        user_a = self.create_saved_test_user_with_checkpoint()
        user_b = self.create_saved_test_user_with_checkpoint()
        self.befriend_test_user(user_a, [user_b])

        # build data for request
        data = {
            "user_id": user_a.user_obj.id,
            "signature": gen_signature(
                "get", "checkpoint", gen_api_key(user_a.user_obj.access_token, user_a.user_obj.id)
            ),
            "type": "search",
            "keyword": user_b.checkpoint_obj.name,
        }

        response = self.client.get("/checkpoint/?" + urllib.urlencode(data))
        assert "ok" in response.data
        assert user_b.checkpoint_obj.name in response.data
Beispiel #13
0
 def test_new_share(self):
     """
     tests API for instantiating a new share (share/put)
     """
     
     test_user_with_checkpoint = self.create_saved_test_user_with_checkpoint()
     another_test_user_to_share = self.create_saved_test_user()
     
     data = {"user_id": test_user_with_checkpoint.user_obj.id,
             "to_user_id": another_test_user_to_share.user_obj.id,
             "signature": gen_signature("put",
                                        "share",
                                        gen_api_key(test_user_with_checkpoint.user_obj.access_token, 
                                                    test_user_with_checkpoint.user_obj.id)),
             "user_checkpoint_id": test_user_with_checkpoint.user_checkpoint_obj.id
             }
     
     resp = self.client.put("/share/", data=data)
     assert "ok" in resp.data
     assert not get_share_w_attr(test_user_with_checkpoint.user_obj, 
                                 another_test_user_to_share.user_obj, 
                                 test_user_with_checkpoint.user_checkpoint_obj) is None
Beispiel #14
0
 def test_authorization(self):
     """
     tests the authorization of a rest api call
     """
     #create a user
     app_xs_token = get_app_access_token(APP_ID, APP_SECRET)
     test_user = create_test_user(APP_ID, app_xs_token)
     auth_code = "someauthcode"
     fb_user_info, user = save_user(test_user["access_token"], auth_code)
     user_id = user.id
     api_key = gen_api_key(test_user["access_token"], user_id)
     
     #create signature
     signature = "\n".join([auth_code, "put", "checkpoint"])
     encrypted_sig = hmac.new(api_key, signature)
     base64_sig = base64.encodestring(encrypted_sig.hexdigest())
     
     #using user's credentials to access new_checkpoint api
     data = {"signature": base64_sig,
             "user_id": user_id}
     resp = self.client.put("/checkpoint/", data=data)
     
Beispiel #15
0
    def test_get_checkpoint_mine(self):
        """
        test the my-checkpoints functionality
                getNewCheckpointBean().getUserBean().getUserId()).toString()));y of (get:checkpoint) api
        """
        # create users
        user_a = self.create_saved_test_user_with_checkpoint()
        user_b = self.create_saved_test_user_with_checkpoint()
        self.befriend_test_user(user_a, [user_b])

        data = {
            "user_id": user_a.user_obj.id,
            "signature": gen_signature(
                "get", "checkpoint", gen_api_key(user_a.user_obj.access_token, user_a.user_obj.id)
            ),
            "type": "mine",
        }

        response = self.client.get("/checkpoint/?" + urllib.urlencode(data))
        assert "ok" in response.data
        assert user_a.checkpoint_obj.name in response.data
        assert not user_b.checkpoint_obj.name in response.data
Beispiel #16
0
    def test_new_comment(self):
        """
        Unit Test API for instantiating a new Comment (comment/put)
        """
        test_user_with_checkpoint = self.create_saved_test_user_with_checkpoint()
        annoying_user_that_likes_everything = self.create_saved_test_user()

        # prep api call
        data = {
            "user_id": annoying_user_that_likes_everything.user_obj.id,
            "signature": gen_signature(
                "put",
                "comment",
                gen_api_key(
                    annoying_user_that_likes_everything.user_obj.access_token,
                    annoying_user_that_likes_everything.user_obj.id,
                ),
            ),
            "user_checkpoint_id": test_user_with_checkpoint.user_checkpoint_obj.id,
            "comment": random_string(),
        }

        resp = self.client.put("/comment/", data=data)
        assert "comment_id" in resp.data