Ejemplo n.º 1
0
def test_digest_task(client, database, smtp_server):
    # Here because it requires the app context to be set
    from monolith.task import send_digest

    no_stories = 'Start following your favourite users to get ' \
                 'periodic digests.'
    send_digest.apply()
    for msg in smtp_server.messages:
        assert msg.get_payload() == no_stories

    now = dt.datetime.now()
    s1 = Story(author_id=2,
               text='Story 1',
               likes=42,
               dislikes=69,
               is_draft=False,
               deleted=False)
    s1.dice_set = ['dice', 'set']
    s1.date = now - dt.timedelta(days=1)
    database.session.add(s1)

    s2 = Story(author_id=2,
               text='Story 2',
               likes=42,
               dislikes=69,
               is_draft=False,
               deleted=False)
    s2.dice_set = ['dice', 'set']
    s2.date = now - dt.timedelta(days=60)
    database.session.add(s2)

    s3 = Story(author_id=3,
               text='Story 3',
               likes=42,
               dislikes=69,
               is_draft=False,
               deleted=False)
    s3.dice_set = ['dice', 'set']
    s3.date = now - dt.timedelta(days=4)
    database.session.add(s3)

    u1 = User.query.get(1)
    u2 = User.query.get(2)
    u3 = User.query.get(3)
    u4 = User.query.get(4)
    u1.follows.append(u2)
    u2.follows.append(u3)
    u3.follows.append(u4)
    database.session.commit()

    smtp_server.reset()
    send_digest.apply()
    for msg in smtp_server.messages:
        u = User.query.filter_by(email=msg['To']).one()
        digest = digests.build_user_digest(u, now - dt.timedelta(weeks=4), now)
        payload = msg.get_payload().replace('\r\n', '\n').strip()
        digest = digest.get_payload().replace('\r\n', '\n').strip()
        assert payload == digest
Ejemplo n.º 2
0
def test_get_random_story(client, database, templates, story_actions):
    example = Story()
    example.text = 'very not recent story (months/years ago)'
    example.likes = 0
    example.author_id = 1
    example.date = dt.datetime(2019, 9, 5)
    example.is_draft = False
    example.deleted = False
    example.dice_set = ['a', 'b', 'c']
    database.session.add(example)

    example = Story()
    example.text = 'not recent story (yesterday)'
    example.date = dt.datetime.now() - dt.timedelta(days=1)
    example.likes = 0
    example.author_id = 2
    example.is_draft = False
    example.deleted = False
    example.dice_set = ['a', 'b', 'c']
    database.session.add(example)

    example = Story()
    example.text = 'drafted story'
    example.date = dt.datetime.now() - dt.timedelta(days=1)
    example.likes = 0
    example.author_id = 1
    example.is_draft = True
    example.deleted = False
    example.dice_set = ['a', 'b', 'c']
    database.session.add(example)

    example = Story()
    example.text = 'deleted story'
    example.date = dt.datetime.now() - dt.timedelta(days=1)
    example.likes = 0
    example.author_id = 1
    example.is_draft = False
    example.deleted = True
    example.dice_set = ['a', 'b', 'c']
    database.session.add(example)

    database.session.commit()

    # story found
    reply = story_actions.get_random_recent_story()
    assert reply.status_code == 200

    template_context = templates[-1]
    id = template_context['story'].id
    message = template_context['message']
    assert id == 1 or id == 2
    assert message == 'no stories today. Here is a random one:'
Ejemplo n.º 3
0
    def test_random_recent_story(self):

        # Random recent story as anonymous user
        self.client.get('/stories/random', follow_redirects=True)
        self.assert_template_used('story.html')
        self.assertEqual(self.get_context_variable('story').text, 'Just another story')

        # Login as Admin
        payload = {'email': '*****@*****.**', 'password': '******'}
        form = LoginForm(data=payload)
        self.client.post('/users/login', data=form.data, follow_redirects=True)

        # No recent stories
        self.client.get('/stories/random', follow_redirects=True)
        self.assert_template_used('stories.html')
        self.assert_message_flashed('Oops, there are no recent stories by other users!')

        # Create a new recent story by Admin2
        example = Story()
        example.text = 'This is a valid recent story'
        example.date = datetime.datetime.now()
        example.author_id = 2
        example.figures = 'story#recent'
        example.is_draft = False
        db.session.add(example)
        db.session.commit()

        # Get the only recent story not written by Admin
        response = self.client.get('/stories/random', follow_redirects=True)
        self.assert_template_used('story.html')
        self.assertEqual(self.get_context_variable('story').text, 'This is a valid recent story')
Ejemplo n.º 4
0
def init_database(database):
    example = Story()
    example.text = 'lorem ipsum dolor sit amet'
    example.likes = 42
    example.author_id = 1
    example.date = dt.datetime(year=2018, month=12, day=1)
    example.is_draft = False
    example.deleted = False
    example.dice_set = ['a', 'b', 'c']
    database.session.add(example)

    example = Story()
    example.text = 'bird drink coffee baloon'
    example.likes = 42
    example.author_id = 1
    example.date = dt.datetime(year=2019, month=1, day=1)
    example.is_draft = False
    example.deleted = False
    example.dice_set = ['a', 'b', 'c']
    database.session.add(example)

    example = Story()
    example.text = 'lorem Coffee dolor sit amet'
    example.likes = 42
    example.author_id = 1
    example.date = dt.datetime(year=2019, month=3, day=12)
    example.is_draft = False
    example.deleted = False
    example.dice_set = ['a', 'b', 'c']
    database.session.add(example)

    example = Story()
    example.text = 'bird cofFee baloon amet'
    example.likes = 42
    example.author_id = 1
    example.date = dt.datetime(year=2017, month=10, day=1)
    example.is_draft = False
    example.deleted = False
    example.dice_set = ['a', 'b', 'c']
    database.session.add(example)

    database.session.commit()
Ejemplo n.º 5
0
def test_get_by_interest(client, auth, database, templates, story_actions):
    auth.login()

    example1 = Story()
    example1.theme = 'halloween'
    example1.text = 'Halloween story of test1 user :)'
    example1.author_id = 2
    example1.is_draft = False
    example1.deleted = False
    example1.dice_set = ['a', 'b', 'c']
    database.session.add(example1)
    database.session.commit()

    example2 = Story()
    example2.theme = 'xmas'
    example2.text = 'Xmas story of test2 user :)'
    example2.author_id = 3
    example2.is_draft = False
    example2.deleted = False
    example2.dice_set = ['a', 'b', 'c']
    database.session.add(example2)
    database.session.commit()

    example3 = Story()
    example3.theme = 'xmas'
    example3.theme = 'Old xmas story of test3 user :)'
    example3.date = dt.datetime.now() - dt.timedelta(days=6)
    example3.author_id = 4
    example3.is_draft = False
    example3.deleted = False
    example3.dice_set = ['a', 'b', 'c']
    database.session.add(example3)
    database.session.commit()

    reply = story_actions.get_all_stories(theme='xmas')
    assert reply.status_code == 200
    assert templates[-1]['stories'].all() == [example2]
Ejemplo n.º 6
0
def test_story_digest_format():
    u = User()
    u.username = '******'
    u.email = '*****@*****.**'
    u.firstname = 'Firstname'
    u.lastname = 'Lastname'

    date = dt.datetime.now()
    s = Story()
    s.author = u
    s.text = 'Lorem ipsum dolor sit amet'
    s.date = date
    s.likes = 42
    s.dislikes = 69
    s.dice_set = ['lorem', 'ipsum', 'dolor', 'sit', 'amet']
    s.deleted = False
    s.is_draft = False

    lines = digests.story_digest(s).split('\n')
    assert lines[0] == 'author: Firstname Lastname (Username)'
    assert lines[1] == 'roll: lorem, ipsum, dolor, sit, amet'
    assert lines[2] == f'date: {date}'
    assert lines[3] == 'story: Lorem ipsum dolor sit amet'
    assert lines[4] == 'likes: 42 -- dislikes: 69'
Ejemplo n.º 7
0
def test_user_digest_format(client, database):
    now = dt.datetime.now()

    s1 = Story(author_id=2,
               text='Story 1',
               likes=42,
               dislikes=69,
               is_draft=False,
               deleted=False)
    s1.dice_set = ['dice', 'set']
    s1.date = now - dt.timedelta(days=1)
    database.session.add(s1)

    s2 = Story(author_id=2,
               text='Story 2',
               date=now,
               likes=42,
               dislikes=69,
               is_draft=False,
               deleted=False)
    s2.dice_set = ['dice', 'set']
    s2.date = now
    database.session.add(s2)

    s3 = Story(author_id=2,
               text='Story 3',
               likes=42,
               dislikes=69,
               is_draft=False,
               deleted=False)
    s3.dice_set = ['dice', 'set']
    s3.date = now - dt.timedelta(days=7)
    database.session.add(s3)

    s4 = Story(author_id=3,
               text='Story 4',
               likes=42,
               dislikes=69,
               is_draft=False,
               deleted=False)
    s4.dice_set = ['dice', 'set']
    s4.date = now
    database.session.add(s4)

    s5 = Story(author_id=2,
               text='Story 5',
               likes=42,
               dislikes=69,
               is_draft=True,
               deleted=False)
    s5.dice_set = ['dice', 'set']
    s5.date = now
    database.session.add(s5)

    s6 = Story(author_id=2,
               text='Story 5',
               likes=42,
               dislikes=69,
               is_draft=False,
               deleted=True)
    s6.dice_set = ['dice', 'set']
    s6.date = now
    database.session.add(s6)

    u = User.query.get(1)
    u.follows.append(User.query.get(2))
    database.session.commit()

    digest = digests.build_user_digest(u, now - dt.timedelta(days=2),
                                       now + dt.timedelta(days=2)) \
                    .get_content().split('----------\n')
    assert len(digest) == 2
    assert digest[0] == digests.story_digest(s2)
    assert digest[1] == digests.story_digest(s1)