def profile(username):
    '''
    * Displays a user's profile page, with extra features if it belongs
      to the user themself.

    \n Args:
    1. username (str): The specific username registered to a user in
       the db.

    \n Returns:
    * The user's profile template, complete with all their competition entries,
      all the photos that user has voted for and all that user's award-
      winning photos, displayed in three different sections.
    * If the user is logged in and viewing their own profile page, they
      will see an 'edit profile' button, as well as information about the next
      stage of the competition and links to bring them to the compete or
      vote templates.
    '''
    user_profile_photo, user_photos, \
        photos_voted_for_objs, award_winners,\
        user_entry_this_comp = \
        get_profile_page_photos(username, mongo)

    user = mongo.db.users.find_one({"username": username})
    can_enter = user["can_enter"]
    votes_to_use = user["votes_to_use"]

    today = datetime.now().strftime('%Y-%m-%d')

    competition_ends = get_next_weekday(today, 5)
    next_competition_starts = get_next_weekday(today, 1)
    voting_ends = get_next_weekday(today, 7) - timedelta(hours=2)

    comp_closes, voting_closes, next_comp_starts = \
        time_strings_for_template(
                competition_ends, next_competition_starts,
                voting_ends, get_time_remaining_string)

    source_url = request.referrer

    return render_template("profile.html",
                           username=username,
                           user=user,
                           user_profile_photo=user_profile_photo,
                           user_photos=user_photos,
                           photos_voted_for=photos_voted_for_objs,
                           award_winners=award_winners,
                           user_entry_this_comp=user_entry_this_comp,
                           can_enter=can_enter,
                           votes_to_use=votes_to_use,
                           comp_closes=comp_closes,
                           voting_closes=voting_closes,
                           next_comp_starts=next_comp_starts,
                           source_url=source_url)
示例#2
0
async def test_get_schedules_request(
    get_schedules_response: MagicMock, ) -> None:
    """Unit test-cases for /switcher/get_schedules request.

    Args:
      get_schedules_response: fixture of mocked
        ``SwitcherV2GetScheduleResponseMSG`` object.
    """
    with patch(
            "request_handlers.SwitcherV2Api.get_schedules",
            return_value=get_schedules_response,
    ):
        async with ClientSession() as session:
            async with session.get(URL_GET_SCHEDULES) as response:
                assert response.status == 200

                body = await response.json()
                assert body[consts.KEY_SUCCESSFUL]
                assert body[consts.KEY_FOUND_SCHEDULES]
                assert len(body[consts.KEY_SCHEDULES]) == 1
                assert (body[consts.KEY_SCHEDULES][0][consts.KEY_SCHEDULE_ID]
                        == consts.DUMMY_SCHEDULE_ID)
                assert body[consts.KEY_SCHEDULES][0][consts.KEY_ENABLED]
                assert body[consts.KEY_SCHEDULES][0][consts.KEY_RECURRING]

                assert len(body[consts.KEY_SCHEDULES][0][consts.KEY_DAYS]) == 1
                assert (body[consts.KEY_SCHEDULES][0][consts.KEY_DAYS][0] ==
                        WEEKDAY_TUP[get_next_weekday()])

                assert (body[consts.KEY_SCHEDULES][0][consts.KEY_START_TIME] ==
                        consts.DUMMY_START_TIME)
                assert (body[consts.KEY_SCHEDULES][0][consts.KEY_END_TIME] ==
                        consts.DUMMY_END_TIME)
                assert (body[consts.KEY_SCHEDULES][0][consts.KEY_DURATION] ==
                        consts.DUMMY_DURATION)

            get_schedules_response.successful = False
            async with session.get(URL_GET_SCHEDULES) as response:
                assert response.status == 200

                body = await response.json()
                assert not body[consts.KEY_SUCCESSFUL]
示例#3
0
def mock_schedule_object() -> Generator[None, None, SwitcherV2Schedule]:
    """Fixture for the aioswitcher.schedules.SwitcherV2Schedule object.

    Returns:
      Mocked ``SwitcherV2Schedule`` object.

    """
    mock_object = MagicMock(SwitcherV2Schedule)
    mock_object.schedule_id = consts.DUMMY_SCHEDULE_ID
    mock_object.enabled = True
    mock_object.recurring = True
    mock_object.days = [WEEKDAY_TUP[get_next_weekday()]]
    mock_object.start_time = consts.DUMMY_START_TIME
    mock_object.end_time = consts.DUMMY_END_TIME
    mock_object.duration = consts.DUMMY_DURATION

    mock_initial_response = MagicMock(SwitcherV2Schedule)
    mock_initial_response.init_future = Future()
    mock_initial_response.init_future.set_result(mock_object)

    return mock_initial_response
示例#4
0
async def test_create_schedule_request(
    create_schedule_response: MagicMock, ) -> None:
    """Unit test-cases for /switcher/create_schedule request.

    Args:
      create_schedule_response: fixture of mocked
        ``SwitcherV2CreateScheduleResponseMSG`` object.
    """
    with patch(
            "request_handlers.SwitcherV2Api.create_schedule",
            return_value=create_schedule_response,
    ):
        async with ClientSession() as session:
            selected_test_day = WEEKDAY_TUP[get_next_weekday()]
            async with session.put(
                    URL_CREATE_SCHEDULE,
                    **{
                        "json": {
                            consts.PARAM_DAYS: [selected_test_day],
                            consts.PARAM_START_HOURS: "20",
                            consts.PARAM_START_MINUTES: "0",
                            consts.PARAM_STOP_HOURS: "20",
                            consts.PARAM_STOP_MINUTES: "30",
                        }
                    },
            ) as response:
                assert response.status == 200

                body = await response.json()
                assert body[consts.KEY_SUCCESSFUL]

            async with session.put(
                    URL_CREATE_SCHEDULE,
                    **{
                        "json": {
                            consts.PARAM_DAYS: [selected_test_day],
                            consts.PARAM_START_HOURS: "24",
                            consts.PARAM_START_MINUTES: "0",
                            consts.PARAM_STOP_HOURS: "20",
                            consts.PARAM_STOP_MINUTES: "30",
                        }
                    },
            ) as response:
                assert response.status == 400
                body = await response.text()
                bs4scrap = BeautifulSoup(body, "html.parser")
                assert (bs4scrap.text ==
                        "Error: Unknown start_hours, accepts 0 to 23.")

            async with session.put(
                    URL_CREATE_SCHEDULE,
                    **{
                        "json": {
                            consts.PARAM_DAYS: [selected_test_day],
                            consts.PARAM_START_HOURS: "23",
                            consts.PARAM_START_MINUTES: "60",
                            consts.PARAM_STOP_HOURS: "20",
                            consts.PARAM_STOP_MINUTES: "30",
                        }
                    },
            ) as response:
                assert response.status == 400
                body = await response.text()
                bs4scrap = BeautifulSoup(body, "html.parser")
                assert (bs4scrap.text ==
                        "Error: Unknown start_minutes, accepts 0 to 59.")

            async with session.put(
                    URL_CREATE_SCHEDULE,
                    **{
                        "json": {
                            consts.PARAM_DAYS: [selected_test_day],
                            consts.PARAM_START_HOURS: "23",
                            consts.PARAM_START_MINUTES: "0",
                            consts.PARAM_STOP_HOURS: "24",
                            consts.PARAM_STOP_MINUTES: "30",
                        }
                    },
            ) as response:
                assert response.status == 400
                body = await response.text()
                bs4scrap = BeautifulSoup(body, "html.parser")
                assert (bs4scrap.text ==
                        "Error: Unknown stop_hours, accepts 0 to 23.")

            async with session.put(
                    URL_CREATE_SCHEDULE,
                    **{
                        "json": {
                            consts.PARAM_DAYS: [selected_test_day],
                            consts.PARAM_START_HOURS: "23",
                            consts.PARAM_START_MINUTES: "0",
                            consts.PARAM_STOP_HOURS: "20",
                            consts.PARAM_STOP_MINUTES: "60",
                        }
                    },
            ) as response:
                assert response.status == 400
                body = await response.text()
                bs4scrap = BeautifulSoup(body, "html.parser")
                assert (bs4scrap.text ==
                        "Error: Unknown stop_minutes, accepts 0 to 59.")

            async with session.put(
                    URL_CREATE_SCHEDULE,
                    **{
                        "json": {
                            consts.PARAM_DAYS: [selected_test_day],
                            consts.PARAM_START_MINUTES: "0",
                            consts.PARAM_STOP_HOURS: "20",
                            consts.PARAM_STOP_MINUTES: "30",
                        }
                    },
            ) as response:
                assert response.status == 400
                body = await response.text()
                bs4scrap = BeautifulSoup(body, "html.parser")
                assert (
                    bs4scrap.text == "Error: Argument start_hours is missing.")

            async with session.put(
                    URL_CREATE_SCHEDULE,
                    **{
                        "json": {
                            consts.PARAM_DAYS: [selected_test_day],
                            consts.PARAM_START_HOURS: "23",
                            consts.PARAM_STOP_HOURS: "20",
                            consts.PARAM_STOP_MINUTES: "30",
                        }
                    },
            ) as response:
                assert response.status == 400
                body = await response.text()
                bs4scrap = BeautifulSoup(body, "html.parser")
                assert (bs4scrap.text ==
                        "Error: Argument start_minutes is missing.")

            async with session.put(
                    URL_CREATE_SCHEDULE,
                    **{
                        "json": {
                            consts.PARAM_DAYS: [selected_test_day],
                            consts.PARAM_START_HOURS: "23",
                            consts.PARAM_START_MINUTES: "0",
                            consts.PARAM_STOP_MINUTES: "30",
                        }
                    },
            ) as response:
                assert response.status == 400
                body = await response.text()
                bs4scrap = BeautifulSoup(body, "html.parser")
                assert (
                    bs4scrap.text == "Error: Argument stop_hours is missing.")

            async with session.put(
                    URL_CREATE_SCHEDULE,
                    **{
                        "json": {
                            consts.PARAM_DAYS: [selected_test_day],
                            consts.PARAM_START_HOURS: "23",
                            consts.PARAM_START_MINUTES: "0",
                            consts.PARAM_STOP_HOURS: "20",
                        }
                    },
            ) as response:
                assert response.status == 400
                body = await response.text()
                bs4scrap = BeautifulSoup(body, "html.parser")
                assert (bs4scrap.text ==
                        "Error: Argument stop_minutes is missing.")

            async with session.put(
                    URL_CREATE_SCHEDULE,
                    **{
                        "json": {
                            consts.PARAM_DAYS: ["Fakeday"],
                            consts.PARAM_START_HOURS: "20",
                            consts.PARAM_START_MINUTES: "0",
                            consts.PARAM_STOP_HOURS: "20",
                            consts.PARAM_STOP_MINUTES: "30",
                        }
                    },
            ) as response:
                assert response.status == 400
                body = await response.text()
                bs4scrap = BeautifulSoup(body, "html.parser")
                assert (
                    bs4scrap.text ==
                    "Error: Unrecognized day requests, check documentation.")

            async with session.put(URL_CREATE_SCHEDULE) as response:
                assert response.status == 400
                body = await response.text()
                bs4scrap = BeautifulSoup(body, "html.parser")
                assert bs4scrap.text == "Error: Json body is missing."

            create_schedule_response.msg_type = ResponseMessageType.STATE
            async with session.put(
                    URL_CREATE_SCHEDULE,
                    **{
                        "json": {
                            consts.PARAM_DAYS: [selected_test_day],
                            consts.PARAM_START_HOURS: "20",
                            consts.PARAM_START_MINUTES: "0",
                            consts.PARAM_STOP_HOURS: "20",
                            consts.PARAM_STOP_MINUTES: "30",
                        }
                    },
            ) as response:
                assert response.status == 200

                body = await response.json()
                assert not body[consts.KEY_SUCCESSFUL]
                assert consts.KEY_MESSAGE in body