Beispiel #1
0
    def test_languages_get(self):
        admin = self.create_user()
        login_response = self.login()

        # remove existing languages in order to avoid db IntegrityError
        self.remove_langs()

        languages = [lang for lang in langs.isoLangs if len(lang) == 2]
        self.create_languages(langs_list=languages)

        # 2 full pages + 1 page with 1 item
        number_of_items = 2 * self.items_per_page + 1

        for i in range(number_of_items):
            # descending number of sentiments generated
            # as we loop over list of languages
            Sentiment.generate_fake(count=(number_of_items - i),
                                    language_code=languages[i % len(languages)])

        lang_page1 = Language.query.filter_by(
            code=languages[0]).first()
        lang_page2 = Language.query.filter_by(
            code=languages[self.items_per_page]).first()
        lang_page3 = Language.query.filter_by(
            code=languages[number_of_items % len(languages)]).first()

        response_page1 = self.client.get(url_for('admin.languages',
                                                 page=1),
                                         follow_redirects=True)
        data_page1 = response_page1.get_data(as_text=True)

        response_page2 = self.client.get(url_for('admin.languages',
                                                 page=2),
                                         follow_redirects=True)
        data_page2 = response_page2.get_data(as_text=True)

        response_page3 = self.client.get(url_for('admin.languages',
                                                 page=3),
                                         follow_redirects=True)
        data_page3 = response_page3.get_data(as_text=True)

        self.assertIn('{lang_name}, {lang_count} sentiments'.
                      format(lang_name=lang_code_to_lang_name(lang_page1.code),
                             lang_count=lang_page1.sentiments.count()),
                      data_page1,
                      'Failed to show language with largest number of '
                      'sentiments in it on the first page.')

        self.assertIn('{lang_name}, {lang_count} sentiments'.
                      format(lang_name=lang_code_to_lang_name(lang_page2.code),
                             lang_count=lang_page2.sentiments.count()),
                      data_page2,
                      'Failed to show language on the proper page.')

        self.assertIn('{lang_name}, {lang_count} sentiments'.
                      format(lang_name=lang_code_to_lang_name(lang_page3.code),
                             lang_count=lang_page3.sentiments.count()),
                      data_page3,
                      'Failed to show language with the least number '
                      'of sentiments on the last page.')
Beispiel #2
0
    def test_edit_sentiment_post(self, mock_form, mock_sentiment, mock_db):
        mock_form.return_value = MockSentimentForm()
        mock_sentiment.return_value = MockSentimentModel()
        mock_db.side_effect = lambda *args, **kwargs: True

        admin = self.create_user()
        login_response = self.login()

        Sentiment.generate_fake(count=10)
        sentiment = Sentiment.query.first()

        text = forgery_py.lorem_ipsum.sentences(5)
        latin = Language.query.filter_by(code='la').first()

        submit_response = self.client.post(url_for('admin.edit_sentiment', id=sentiment.id),
                                           data=dict(body=text,
                                                     score=random.random(),
                                                     language=latin.id,
                                                     timestamp=datetime.utcnow()),
                                           follow_redirects=False)
        data_submit = submit_response.get_data(as_text=True)

        mock_form.assert_called()

        self.assertIn('Redirecting.', data_submit,
                      'Failed to update sentiment edited by a valid user.')
Beispiel #3
0
    def test_sentiments_get_pagination(self):
        admin = self.create_user()
        login_response = self.login()

        Sentiment.generate_fake(count=((self.items_per_page * 2) + 2))
        sentiments = Sentiment.query.order_by(Sentiment.timestamp.desc())

        response_page1 = self.client.get(url_for('admin.sentiments'),
                                         follow_redirects=True)
        data_page1 = response_page1.get_data(as_text=True)

        response_page2 = self.client.get(url_for('admin.sentiments', page=2),
                                         follow_redirects=True)
        data_page2 = response_page2.get_data(as_text=True)

        response_page3 = self.client.get(url_for('admin.sentiments', page=3),
                                         follow_redirects=True)
        data_page3 = response_page3.get_data(as_text=True)

        self.assertIn(sentiments.paginate(page=1,
                                          per_page=self.items_per_page).
                      items[0].body_html,
                      data_page1,
                      'Failed to show a sentiment on the proper page.')
        self.assertIn(sentiments.paginate(page=2,
                                          per_page=self.items_per_page).
                      items[0].body_html,
                      data_page2)
        self.assertIn(sentiments.paginate(page=3,
                                          per_page=self.items_per_page).
                      items[0].body_html,
                      data_page3)
Beispiel #4
0
    def test_edit_sentiment_get_admin(self):
        admin = self.create_user()
        Sentiment.generate_fake(count=10)
        sentiment_admin = Sentiment.query.first()

        login_response = self.login()
        response = self.client.get(url_for('admin.edit_sentiment', id=sentiment_admin.id),
                                   follow_redirects=True)
        data = response.get_data(as_text=True)
        self.assertIn(sentiment_admin.body,
                      data, 'Failed to show sentiment editing form to an admin.')
Beispiel #5
0
    def test_edit_sentiment_get_author_non_admin(self):
        user = self.create_user(role_name='Moderator', email='*****@*****.**',
                                username='******', password='******')
        Sentiment.generate_fake(count=10)
        sentiment_user = Sentiment.query.first()

        login_response = self.login(email='*****@*****.**', password='******')
        response = self.client.get(url_for('admin.edit_sentiment', id=sentiment_user.id),
                                   follow_redirects=True)
        data = response.get_data(as_text=True)
        self.assertIn(sentiment_user.body,
                      data, 'Failed to show sentiment editing form to an non-admin author.')
Beispiel #6
0
    def test_remove_sentiment_admin(self):
        admin = self.create_user()
        login_response = self.login()

        Sentiment.generate_fake(count=10)
        sentiment = Sentiment.query.first()

        response = self.client.get(url_for('admin.remove_sentiment',
                                           id=sentiment.id),
                                   follow_redirects=True)
        data = response.get_data(as_text=True)

        self.assertIn('The sentiment has been removed', data,
                      'Failed to allow sentiment removal by an administrator.')
Beispiel #7
0
    def test_edit_sentiment_get_neither_admin_nor_author(self):
        admin = self.create_user()
        Sentiment.generate_fake(count=10)
        sentiment = Sentiment.query.first()

        user = self.create_user(role_name='Moderator', email='*****@*****.**',
                                username='******', password='******')

        login_response = self.login(email='*****@*****.**', password='******')

        response = self.client.get(url_for('admin.edit_sentiment', id=sentiment.id),
                                   follow_redirects=True)
        data = response.get_data(as_text=True)
        self.assertIn('can be edited by either its author or a site administrator',
                      data, 'Failed to forbid editing to a user who is neither '
                            'an author nor administrator.')
Beispiel #8
0
    def test_remove_sentiment_neither_author_nor_admin(self):
        user1 = self.create_user(role_name='Moderator',
                                 email='*****@*****.**',
                                 username='******',
                                 password='******')
        user2 = self.create_user(role_name='Moderator',
                                 email='*****@*****.**',
                                 username='******',
                                 password='******')

        latin = Language.query.filter_by(code='la').first()
        text = forgery_py.lorem_ipsum.sentences(5)

        sentiment = Sentiment(author=user1,
                              language_id=latin.id,
                              body=text,
                              score=0.5,
                              timestamp=datetime.utcnow())

        user2_login_response = self.login(email='*****@*****.**',
                                          password='******')

        response = self.client.get(url_for('admin.remove_sentiment',
                                           id=sentiment.id),
                                   follow_redirects=True)
        data = response.get_data(as_text=True)

        self.assertIn('either its author or a site administrator', data,
                      'Failed to forbid sentiment removal for a user '
                      'who neither an author nor administrator.')
Beispiel #9
0
    def test_select_db_sentiment(self):
        parsed_update = parse_update(TelegramUpdates.TEXT_OK_ID_OK_TEXT)
        parsed_update['score'] = 0.678
        parsed_update['language'] = 'la'
        Sentiment.generate_fake(count=2,
                                subsequent_scores=True,
                                levels=[0.678, 0.789, 0.90, 1.0])

        expected_sentiment = Sentiment.query.filter(
            Sentiment.score == parsed_update['score']).first()
        not_expected_sentiment = Sentiment.query.filter(
            Sentiment.score == 0.789).first()

        result = select_db_sentiment.delay(parsed_update).get(timeout=5)

        self.assertEqual(result['text'], expected_sentiment.body_html)
        self.assertNotEqual(result['text'], not_expected_sentiment.body_html)
        self.assertEqual(result['parse_mode'], 'HTML')
Beispiel #10
0
    def test_language_get(self):
        admin = self.create_user()
        login_response = self.login()

        # 2 full pages + 1 page with 1 item on it
        number_of_items = 2 * self.items_per_page + 1
        Sentiment.generate_fake(count=number_of_items)

        latin = Language.query.filter_by(code='la').first()

        sentiments_page1 = Sentiment.query.\
            filter_by(language=latin).paginate(page=1,
                                               per_page=self.items_per_page)
        sentiments_page2 = Sentiment.query.\
            filter_by(language=latin).paginate(page=2,
                                               per_page=self.items_per_page)
        sentiments_page3 = Sentiment.query.\
            filter_by(language=latin).paginate(page=3,
                                               per_page=self.items_per_page)

        response_page1 = self.client.get(url_for('admin.language',
                                                 code='la',
                                                 page=1),
                                         follow_redirects=True)
        data_page1 = response_page1.get_data(as_text=True)

        response_page2 = self.client.get(url_for('admin.language',
                                                 code='la',
                                                 page=2),
                                         follow_redirects=True)
        data_page2 = response_page2.get_data(as_text=True)

        response_page3 = self.client.get(url_for('admin.language',
                                                 code='la',
                                                 page=3),
                                         follow_redirects=True)
        data_page3 = response_page3.get_data(as_text=True)

        self.assertIn(sentiments_page1.items[0].body_html,
                      data_page1)
        self.assertIn(sentiments_page2.items[0].body_html,
                      data_page2)
        self.assertIn(sentiments_page3.items[0].body_html,
                      data_page3)
Beispiel #11
0
    def test_sentiment_json(self):
        User.generate_fake(count=2)
        Sentiment.generate_fake(count=1)
        s = Sentiment.query.first()
        json_data = s.to_json()

        self.assertEqual(json_data['body'], s.body)
        self.assertEqual(json_data['body_html'], s.body_html)
        self.assertEqual(json_data['timestamp'], s.timestamp)

        sentiment_from_json = Sentiment.from_json(json_data)
        self.assertEqual(sentiment_from_json.body, s.body)

        with self.assertRaises(ValidationError) as err:
            Sentiment.from_json({})

        self.assertIn("sentiment does not have a body", str(err.exception))

        self.assertEqual(repr(s), "<Sentiment '{}'>".format(s.body))
Beispiel #12
0
    def test_remove_language(self):
        admin = self.create_user()
        login_response = self.login()
        latin = Language.query.filter_by(code='la').first()
        Sentiment.generate_fake(count=10, language_code=latin.code)

        submit_response = self.client.post(url_for('admin.remove_language',
                                                   code=latin.code),
                                           follow_redirects=True)
        data_submit = submit_response.get_data(as_text=True)

        self.assertIn('Your language and all associated sentiments have been deleted',
                      data_submit,
                      'Failed to remove language.')

        sentiments = Sentiment.query.filter_by(language=latin).all()
        self.assertEqual(sentiments, [],
                         'Failed to remove sentiments associated '
                         'with the language removed.')
Beispiel #13
0
    def setUp(self):
        """Method called before each unit-test"""
        # create app, set TESTING flag to disable error catching
        self.app = create_app('testing')

        # push app context
        self.app_context = self.app.app_context()
        self.app_context.push()

        # create databases, see config.py for testing db settings
        db.create_all()

        # pre-fill db with minimal needed things
        Role.insert_roles()
        Language.insert_basic_languages()
        User.generate_fake(10)
        Sentiment.generate_fake(25)

        # Werkzeug Client to make requests
        self.client = self.app.test_client(use_cookies=True)
Beispiel #14
0
    def test_select_db_sentiment_plain_text(self, mock_random):
        mock_sentiment = MockSentiment(body='hello',
                                       body_html=None,
                                       score=0.678)
        mock_random.choice.return_value = mock_sentiment

        parsed_update = parse_update(TelegramUpdates.TEXT_OK_ID_OK_TEXT)
        parsed_update['score'] = 0.678
        parsed_update['language'] = 'la'
        Sentiment.generate_fake(count=2,
                                subsequent_scores=True,
                                levels=[0.678, 0.789, 0.90, 1.0])
        not_expected_sentiment = Sentiment.query.filter(
            Sentiment.score == 0.678).first()

        result = select_db_sentiment.delay(parsed_update).get(timeout=5)

        self.assertEqual(result['text'], mock_sentiment.body)
        self.assertNotEqual(result['text'], not_expected_sentiment.body_html)
        mock_random.choice.assert_called()
Beispiel #15
0
    def test_sentiment_generate_fake(self):
        User.generate_fake(count=10)
        self.assertEqual(Sentiment.query.all(), [])

        levels = [i / 10.0 for i in range(100)]
        Sentiment.generate_fake(count=100,
                                subsequent_scores=False,
                                levels=levels)

        sentiments = Sentiment.query.order_by(Sentiment.id.asc()).all()
        self.assertNotEqual(levels, [s.score for s in sentiments])

        Sentiment.query.filter().delete()
        db.session.commit()

        Sentiment.generate_fake(count=100,
                                subsequent_scores=True,
                                levels=levels)
        sentiments = Sentiment.query.order_by(Sentiment.id.asc()).all()
        self.assertEqual(levels, [s.score for s in sentiments])
Beispiel #16
0
    def test_remove_sentiment_author_non_admin(self):
        user = self.create_user(role_name='Moderator',
                                email='*****@*****.**',
                                username='******',
                                password='******')

        latin = Language.query.filter_by(code='la').first()
        text = forgery_py.lorem_ipsum.sentences(5)

        sentiment = Sentiment(author=user,
                              language_id=latin.id,
                              body=text,
                              score=0.5,
                              timestamp=datetime.utcnow())

        login_response = self.login(email='*****@*****.**',
                                          password='******')

        response = self.client.get(url_for('admin.remove_sentiment',
                                           id=sentiment.id),
                                   follow_redirects=True)
        data = response.get_data(as_text=True)
        self.assertIn('The sentiment has been removed', data,
                      'Failed to allow sentiment removal by its author.')
Beispiel #17
0
    def test_user(self):
        user = self.create_user(email='*****@*****.**',
                                username='******',
                                password='******',
                                invited=True,
                                role_name='Administrator',
                                confirmed=True)
        login_response = self.login(email='*****@*****.**',
                                    password='******',
                                    follow_redirects=True)
        # pagination takes 2 full pages + 1 item on the page 3
        items_per_page = current_app.config['APP_ITEMS_PER_PAGE']
        Sentiment.generate_fake(count=(items_per_page * 2 + 1))

        no_such_user_response = \
            self.client.get(url_for('auth.user',
                                    username='******'),
                            follow_redirects=False)
        self.assertEqual(no_such_user_response.status_code, 404)
        no_such_user_data = no_such_user_response.get_data(as_text=True)
        self.assertNotIn('no_such_user', no_such_user_data)

        page1_response = \
            self.client.get(url_for('auth.user',
                                    username='******', page=1),
                            follow_redirects=True)
        data_page1 = page1_response.get_data(as_text=True)

        sentiments = user.sentiments.order_by(Sentiment.timestamp.desc()).all()
        page1_sentiment = sentiments[0]
        page2_sentiment = sentiments[items_per_page]
        page3_sentiment = sentiments[-1]

        self.assertIn(
            page1_sentiment.body_html, data_page1,
            'Failed to show the oldest sentiment on the first page.')

        page2_response = \
            self.client.get(url_for('auth.user',
                                    username='******', page=2),
                            follow_redirects=True)
        data_page2 = page2_response.get_data(as_text=True)

        self.assertIn(page2_sentiment.body_html, data_page2,
                      'Failed to show the sentiment on the proper page.')

        page3_response = \
            self.client.get(url_for('auth.user',
                                    username='******', page=3),
                            follow_redirects=True)
        data_page3 = page3_response.get_data(as_text=True)

        self.assertIn(page3_sentiment.body_html, data_page3,
                      'Failed to show the sentiment on the proper page.')

        page777_response = \
            self.client.get(url_for('auth.user',
                                    username='******', page=777),
                            follow_redirects=True)
        data_page777 = page777_response.get_data(as_text=True)

        self.assertNotIn(
            page1_sentiment.body_html, data_page777,
            'Failed to show empty list of sentiments '
            'after pagination exhausted.')
        self.assertNotIn(page2_sentiment.body_html, data_page777)
        self.assertNotIn(page3_sentiment.body_html, data_page777)