def test_updating_secret_is_prohibited(self): secret = SecretFactory() secret.key = "somekey" with pytest.raises(PreventModelUpdateException): secret.save()
def test_secret_is_destroyed_after_viewing(self): secret = SecretFactory(content="A secret message for Bob from Alice") # Bob opens the link he got from Alice. response = self.app.get(secret.get_absolute_url_private()) assert response.status == "200 OK" # Bob clicks the confirmation button. response = response.form.submit() assert response.request.method == "POST" # Bob can read the secret. the_secret = response.html.select("#the-secret")[0].text assert the_secret == "A secret message for Bob from Alice" # The secret has been deleted from the database. with pytest.raises(Secret.DoesNotExist): secret.refresh_from_db()
def test_secret_view_when_secret_no_longer_exists(self): secret = SecretFactory(content="A secret message for Bob from Alice") # Bob opens the link he fot from Alice. response = self.app.get(secret.get_absolute_url_private()) assert response.status == "200 OK" # The secret is deleted in the meantime, maybe because Mallory viewed the secret ;-) secret.delete() # Bob clicks the confirmation button. response = response.form.submit() assert response.request.method == "POST" # Bob is told that the secret does not exist. assert response.html.h1.text == "Unknown Secret" assert ( response.html.find_all("div", class_="alert-warning")[0].text.strip() == "The secret message you want to read either never existed or has already been read." )
def test_repeated_view_of_the_secret_renders_hint(self): secret = SecretFactory(content="A secret message for Bob from Alice") # Bob opens the link he got from Alice. response = self.app.get(secret.get_absolute_url_private()) assert response.status == "200 OK" # Bob clicks the confirmation button. response = response.form.submit() assert response.request.method == "POST" # Bob can read the secret. the_secret = response.html.select("#the-secret")[0].text assert the_secret == "A secret message for Bob from Alice" # Bob tries to access the secret a second time. response = self.app.get(secret.get_absolute_url_private()) assert response.status == "200 OK" assert response.html.h1.text == "Unknown Secret" assert ( response.html.find_all("div", class_="alert-warning")[0].text.strip() == "The secret message you want to read either never existed or has already been read." )
def test_viewing_an_expired_secret(self): date_of_creation = datetime(2015, 1, 1, 14, 0, 0, tzinfo=pytz.UTC) date_of_viewing = datetime(2015, 1, 31, 14, 0, 0, tzinfo=pytz.UTC) with freeze_time(date_of_creation) as frozen_datetime: # Alice creates a secret. secret = SecretFactory( content="A secret message for Bob from Alice") # Bob waits a few days before viewing the secret. frozen_datetime.move_to(date_of_viewing) # Bob opens the link he got from Alice. response = self.app.get(secret.get_absolute_url_private()) assert response.status == "200 OK" # Bob is told that the secret does not exist. assert response.html.h1.text == "Unknown Secret" assert ( response.html.find_all( "div", class_="alert-warning")[0].text.strip() == "The secret message you want to read either never existed or has already been read." )
def test_get_absolute_url_sharing(self): uuid = uuid4() secret = SecretFactory(uuid=uuid, key="somekey") assert secret.get_absolute_url_sharing() == f"/private/somekey/{uuid}/"
def test_string_representation(self): secret = SecretFactory() assert str(secret) == "1"
def test_secret_factory(self): secret = SecretFactory() secret.full_clean()