def test_get_current_pin_for_user(self):
        self.pin_single_sample(self.user["id"], 0)
        expected_pinned = db_pinned_rec.get_current_pin_for_user(self.user["id"])
        recieved_pinned = db_pinned_rec.get_pin_history_for_user(user_id=self.user["id"], count=50, offset=0)[0]
        self.assertEqual(recieved_pinned, expected_pinned)

        self.pin_single_sample(self.user["id"], 1)
        expected_pinned = db_pinned_rec.get_current_pin_for_user(self.user["id"])
        recieved_pinned = db_pinned_rec.get_current_pin_for_user(self.user["id"])
        self.assertEqual(recieved_pinned, expected_pinned)
    def test_unpin(self):
        pinned = self.pin_single_sample(self.user["id"], 0)
        db_pinned_rec.unpin(self.user["id"])
        self.assertIsNone(db_pinned_rec.get_current_pin_for_user(self.user["id"]))

        # test that the pinned_until value was updated
        unpinned = db_pinned_rec.get_pin_history_for_user(user_id=self.user["id"], count=50, offset=0)[0]
        self.assertGreater(pinned.pinned_until, unpinned.pinned_until)
Пример #3
0
    def test_delete_unauthorized(self):
        """Tests that delete endpoint returns 401 when auth token is invalid"""
        # pin track for user1
        response = self.client.post(
            url_for("pinned_rec_api_bp_v1.pin_recording_for_user"),
            data=json.dumps(self.pinned_rec_samples[0]),
            headers={"Authorization": "Token {}".format(self.user["auth_token"])},
            content_type="application/json",
        )

        # attempt to delete
        pin_to_delete = db_pinned_rec.get_current_pin_for_user(self.user["id"])

        response = self.client.post(
            url_for("pinned_rec_api_bp_v1.delete_pin_for_user", row_id=pin_to_delete.row_id),
            headers={"Authorization": "Token {}".format("-- This is an invalid auth token --")},
        )
        self.assert401(response)
        self.assertEqual(response.json["code"], 401)
Пример #4
0
    def test_delete(self):
        """Tests that unpin endpoint returns 200 when successful"""
        # pin track before deleting
        response = self.client.post(
            url_for("pinned_rec_api_bp_v1.pin_recording_for_user"),
            data=json.dumps(self.pinned_rec_samples[0]),
            headers={"Authorization": "Token {}".format(self.user["auth_token"])},
            content_type="application/json",
        )

        pin_to_delete = db_pinned_rec.get_current_pin_for_user(self.user["id"])

        response = self.client.post(
            url_for("pinned_rec_api_bp_v1.delete_pin_for_user", row_id=pin_to_delete.row_id),
            headers={"Authorization": "Token {}".format(self.user["auth_token"])},
        )

        self.assert200(response)
        self.assertEqual(response.json["status"], "ok")
Пример #5
0
def profile(user_name):
    # Which database to use to showing user listens.
    db_conn = webserver.timescale_connection._ts
    # Which database to use to show playing_now stream.
    playing_now_conn = webserver.redis_connection._redis

    user = _get_user(user_name)
    # User name used to get user may not have the same case as original user name.
    user_name = user.musicbrainz_id

    # Getting data for current page
    max_ts = request.args.get("max_ts")
    if max_ts is not None:
        try:
            max_ts = int(max_ts)
        except ValueError:
            raise BadRequest("Incorrect timestamp argument max_ts: %s" %
                             request.args.get("max_ts"))

    min_ts = request.args.get("min_ts")
    if min_ts is not None:
        try:
            min_ts = int(min_ts)
        except ValueError:
            raise BadRequest("Incorrect timestamp argument min_ts: %s" %
                             request.args.get("min_ts"))

    args = {}
    if max_ts:
        args['to_ts'] = max_ts
    else:
        args['from_ts'] = min_ts
    data, min_ts_per_user, max_ts_per_user = db_conn.fetch_listens(
        user_name, limit=LISTENS_PER_PAGE, **args)

    listens = []
    for listen in data:
        listens.append(listen.to_api())

    # If there are no previous listens then display now_playing
    if not listens or listens[0]['listened_at'] >= max_ts_per_user:
        playing_now = playing_now_conn.get_playing_now(user.id)
        if playing_now:
            listen = {
                "track_metadata": playing_now.data,
                "playing_now": "true",
            }
            listens.insert(0, listen)

    user_stats = db_stats.get_user_stats(user.id, 'all_time', 'artists')

    logged_in_user_follows_user = None
    already_reported_user = False
    if current_user.is_authenticated:
        logged_in_user_follows_user = db_user_relationship.is_following_user(
            current_user.id, user.id)
        already_reported_user = db_user.is_user_reported(
            current_user.id, user.id)

    pin = get_current_pin_for_user(user_id=user.id)
    if pin:
        pin = dict(fetch_track_metadata_for_pins([pin])[0])

    props = {
        "user": {
            "id": user.id,
            "name": user.musicbrainz_id,
        },
        "listens": listens,
        "latest_listen_ts": max_ts_per_user,
        "oldest_listen_ts": min_ts_per_user,
        "latest_spotify_uri": _get_spotify_uri_for_listens(listens),
        "artist_count": format(user_stats.count, ",d") if user_stats else None,
        "profile_url": url_for('user.profile', user_name=user_name),
        "mode": "listens",
        "userPinnedRecording": pin,
        "web_sockets_server_url": current_app.config['WEBSOCKETS_SERVER_URL'],
        "logged_in_user_follows_user": logged_in_user_follows_user,
        "already_reported_user": already_reported_user,
    }

    return render_template("user/profile.html",
                           props=ujson.dumps(props),
                           mode='listens',
                           user=user,
                           active_section='listens')