Ejemplo n.º 1
0
    def test_invite_single(self, mock_send_email):
        atm = utcnow()

        request = MagicMock()
        request.site = Site.objects.first()

        user_degenerate = self.create_user()
        user_not_verified = self.create_user()
        user_verified = self.create_user(verified=True)

        ap_degenerated = AuthProfile(user=user_degenerate, site=request.site)
        ap_degenerated.save()
        start_verification(request, user_not_verified)
        start_verification(request, user_verified)

        tasks.invite_single_user(user_degenerate.email)
        self.assertFalse(mock_send_email.called)
        ap_degenerated.refresh_from_db()
        self.assertIsNone(ap_degenerated.verified_at)
        self.assertIsNone(ap_degenerated.notified_at)
        self.assertFalse(ap_degenerated.verification_code)

        mock_send_email.reset_mock()
        tasks.invite_single_user(user_not_verified.email)
        ap: AuthProfile = user_not_verified.auth_profile
        mock_send_email.assert_called_once_with(
            context={"link": ap.link, "service": "Sidorov.dev"},
            email_to=user_not_verified.email,
            mail_template_name="invitation",
            subject=f"Registration at Sidorov.dev",
        )
        ap.refresh_from_db()
        self.assertIsNone(ap.verified_at)
        self.assertTrue(near(atm, ap.notified_at, 1))
        self.assertTrue(ap.link)

        mock_send_email.reset_mock()
        tasks.invite_single_user(user_verified.email)
        ap: AuthProfile = user_verified.auth_profile
        mock_send_email.assert_called_once_with(
            context={"link": ap.link, "service": "Sidorov.dev"},
            email_to=user_verified.email,
            mail_template_name="invitation",
            subject=f"Registration at Sidorov.dev",
        )
        ap.refresh_from_db()
        self.assertIsNone(ap.verified_at)
        self.assertTrue(near(atm, ap.notified_at, 1))
        self.assertTrue(ap.link)
Ejemplo n.º 2
0
    def test_post_user(self):
        user = self.create_user()
        client = Client()
        client.login(username=user.username, password=user.username)

        rem = Reminder(creator=user, title=user.username)
        rem.save()

        created_at = rem.created_at
        dtm = utcnow() + timedelta(days=1)

        self.validate_response(
            client=client,
            method="post",
            url=f"{rem.get_absolute_url()}update/",
            form_data={
                "created_at": dtm,
                "status": ReminderStatus.NOTIFIED,
                "title": f"title_{user.username}",
                "participants": [
                    user.pk,
                ],
            },
            expected_view_name="reminders:reminder",
            expected_view=ReminderView,
            expected_redirect_chain=[(rem.get_absolute_url(), 302)],
            expected_template="reminders/reminder.html",
            content_filters=(
                lambda _c: b"error" not in _c,
                lambda _c: f"title_{user.username}".encode() in _c,
                lambda _c: ReminderStatus.NOTIFIED.value.encode() not in _c,
            ),
        )

        rem.refresh_from_db()

        self.assertEqual(rem.title, f"title_{user.username}")
        self.assertEqual(rem.status, ReminderStatus.CREATED.name)
        self.assertEqual(rem.created_at, created_at)
        self.assertNotEqual(rem.created_at, dtm)
Ejemplo n.º 3
0
def finalize_verification(request: HttpRequest, code: Union[str,
                                                            None]) -> bool:
    if not code:
        return False

    try:
        auth = AuthProfile.objects.get(verification_code=code)
    except AuthProfile.DoesNotExist:
        return False

    if auth.is_verified:
        return True

    auth.verified_at = utcnow()
    auth.save()
    auth.user.is_active = True
    auth.user.save()

    setup_profile(auth.user)

    login(request, auth.user)

    return True
Ejemplo n.º 4
0
    def test_post_normal(self):
        placeholder = urandom(4).hex()
        title = f"title_{placeholder}"

        self.validate_response(
            self.endpoint,
            method="post",
            headers=self.auth_headers,
            data={
                "title": title,
            },
            expected_status_code=status.HTTP_201_CREATED,
        )

        reminders = Reminder.objects.filter(title=title)
        self.assertEqual(1, reminders.count())

        reminder = reminders.first()
        self.assertTrue(reminder)

        self.assertEqual(title, reminder.title)
        self.assertEqual(self.user, reminder.creator)
        self.assertTrue(near(utcnow(), reminder.created_at, 4))
        self.assertTrue(ReminderStatus.CREATED.name, reminder.status)
Ejemplo n.º 5
0
    for i in range(16):  # FIXME: magic
        current = start + timedelta(days=i)
        day = Day(date=current,
                  date_s=current.strftime("%a, %B %-d"),
                  number=i + 1)
        days.append(day)

    return tuple(days)


def sync_calendar(calendar: Calendar) -> None:
    if not (url := calendar.ical_url):
        return

    calendar.ical = download(url)
    calendar.synced_at = utcnow()
    calendar.save()


def download(url: str) -> Union[str, None]:
    resp = requests.get(url, timeout=30)  # FIXME: magic
    if resp.status_code != 200:
        return None

    return resp.content.decode()


def extract_events(calendars: Iterable[Calendar],
                   days: DaysT) -> List[icalparser.Event]:
    calendars_with_ical = filter(lambda _c: _c.ical, calendars)
    date_from = days[0].date
Ejemplo n.º 6
0
 def test_utcnow(self):
     atm = utcnow()
     self.assertEqual(
         atm, datetime(year=2020, month=1, day=1, hour=12, tzinfo=pytz.UTC))
Ejemplo n.º 7
0
 def is_verified(self) -> bool:
     cond = self.verified_at and self.verified_at <= utcnow()
     return cond
Ejemplo n.º 8
0
 def is_active(self):
     return (self.notify_at and self.notify_at >= utcnow()
             and not self.is_done and not self.notified_at)