Esempio n. 1
0
    def test_import_tool_forum_import_id(self, tlos, h, session, m, g, c):
        """ This method tests if an import id is added to a forum """

        importer = discussion.ForgeDiscussionImporter()

        # Test with import id
        import_id = "qwert258"
        _json = {
            "forums": [{
                "description": "Forum about anything you want to talk about.",
                "shortname": "general",
                "name": "General Discussion",
                "import_id": import_id,
                "threads": []
            }]
        }

        importer._load_json = mock.Mock(return_value=_json)
        importer.add_posts = mock.Mock()
        importer._clear_forums = mock.Mock()

        project, user = mock.Mock(), mock.Mock()
        app = project.install_app.return_value
        app.config.options.mount_point = 'mount_point'
        app.config.options.import_id = {'source': 'Allura'}
        app.config.options.get = lambda *a: getattr(app.config.options, *a)
        app.url = 'foo'
        app.forums = []

        forum = mock.Mock()
        utils.create_forum = mock.Mock(return_value=forum)
        forum.get_discussion_thread.return_value = (mock.Mock(), mock.Mock())

        utils.create_forum.return_value = forum

        g.post_event = mock.Mock()
        m.AuditLog.log = mock.Mock()

        importer.import_tool(project, user, 'mount_point', 'mount_label')

        self.assertEqual(import_id, forum.import_id)

        # Test without import id
        _json = {
            "forums": [{
                "description": "Forum about anything you want to talk about.",
                "shortname": "general",
                "name": "General Discussion",
                "threads": []
            }]
        }

        importer._load_json = mock.Mock(return_value=_json)
        forum = mock.Mock()
        forum.import_id = ''
        utils.create_forum(return_value=forum)

        importer.import_tool(project, user, 'mount_point', 'mount_label')

        self.assertEqual('', forum.import_id)
Esempio n. 2
0
    def test_annotate_text_with_unicode(self):
        """ This method tests if annotate text can handle unicode characters """

        importer = discussion.ForgeDiscussionImporter()

        # unicode in text of an existing user
        text = "post with un\u00ef\u00e7\u00f8\u2202\u00e9"
        user = mock.Mock(_id=1, is_anonymous=lambda: False)
        username = "******"
        try:
            importer.annotate_text(text, user, username)
        except Exception:
            self.fail("annotate_text() can't handle unicode characters")

        # unicode in text of a not existing user
        text = "post with un\u00ef\u00e7\u00f8\u2202\u00e9"
        user = mock.Mock(is_anonymous=lambda: True)
        username = "******"

        try:
            importer.annotate_text(text, user, username)
        except Exception:
            self.fail("annotate_text() can't handle unicode characters")

        # unicode in text of an anonymous user
        text = "post with un\u00ef\u00e7\u00f8\u2202\u00e9"
        user = mock.Mock(is_anonymous=lambda: True)
        username = "******"

        try:
            importer.annotate_text(text, user, username)
        except Exception:
            self.fail("annotate_text() can't handle unicode characters")
Esempio n. 3
0
    def test_annotate_text_with_existing_user(self):
        """ This test tests the annotate_text method if an existing user is passed as argument """

        importer = discussion.ForgeDiscussionImporter()

        # Creating a mock of an existing user
        user = mock.Mock(_id=1)
        user.is_anonymous.return_value = False
        username = "******"

        self.assertEqual(importer.annotate_text('foo', user, username), 'foo')
Esempio n. 4
0
    def test_annotate_text_with_junk(self):
        """ This test tests if the annotate_text method correctly handles junk data """

        importer = discussion.ForgeDiscussionImporter()

        user = None
        text = None
        username = None

        self.assertEqual(importer.annotate_text(text, user, username), '')

        text = 'foo'
        self.assertEqual(importer.annotate_text(text, user, username), text)
Esempio n. 5
0
    def test_annotate_text_with_anonymous_user(self):
        """ This test tests the annotate_text method with an anonymous user """

        importer = discussion.ForgeDiscussionImporter()

        # Creating an anonymous user
        user = mock.Mock(_id=1)
        user.is_anonymous.return_value = True

        usernames = ['nobody', '*anonymous']

        text = 'foo'

        self.assertEqual(importer.annotate_text(text, user, usernames[0]),
                         text)
        self.assertEqual(importer.annotate_text(text, user, usernames[1]),
                         text)
Esempio n. 6
0
    def test_annotate_text_with_not_existing_user(self):
        """ This test tests the annotate_text method with a not existing user """

        importer = discussion.ForgeDiscussionImporter()

        # Creating a not existing user
        user = mock.Mock(_id=1)
        user.is_anonymous.return_value = True
        username = "******"

        # Text of the post
        text = 'foo'

        # Text that the method should return
        return_text = '*Originally created by:* {username}\n\n{text}'.format(
            username=username, text=text)

        self.assertEqual(importer.annotate_text(text, user, username),
                         return_text)
Esempio n. 7
0
    def __init_add_posts_tests(self):
        importer = discussion.ForgeDiscussionImporter()

        user1 = mock.Mock(_id=1, is_anonymous=lambda: False)

        importer.get_user = mock.Mock(return_value=user1)
        importer.annotate_text = mock.Mock(return_value='foo')

        project, _ = mock.Mock(), mock.Mock()
        app = project.install_app.return_value
        app.config.options.mount_point = 'mount_point'
        app.config.options.import_id = {'source': 'Allura'}
        app.config.options.get = lambda *a: getattr(app.config.options, *a)
        app.url = 'foo'
        app.forums = []

        thread = mock.Mock()
        post = mock.Mock()
        thread.add_post.return_value = post

        return importer, app, thread, user1, post
Esempio n. 8
0
    def test_import_tool_missing_keys(self, tlos, h, session, m, g, c):
        """ This method tests if missing keys are handled correctly """

        # Check when shortname missing
        _json = {
            "forums": [{
                "description": "Forum about anything you want to talk about.",
                "name": "General Discussion",
                "threads": []
            }]
        }

        importer = discussion.ForgeDiscussionImporter()
        importer._load_json = mock.Mock(return_value=_json)

        u = mock.Mock(_id=123, is_anonymous=lambda: False)
        importer.get_user = mock.Mock(return_value=u)

        project, user = mock.Mock(), mock.Mock()
        app = project.install_app.return_value
        app.config.options.mount_point = 'mount_point'
        app.config.options.import_id = {'source': 'Allura'}
        app.config.options.get = lambda *a: getattr(app.config.options, *a)
        app.url = 'foo'
        app.forums = []

        forum = mock.Mock()
        utils.create_forum = mock.Mock(return_value=forum)
        thread = mock.Mock()
        forum.get_discussion_thread.return_value = (thread, mock.Mock())

        utils.create_forum.return_value = forum

        g.post_event = mock.Mock()
        m.AuditLog.log = mock.Mock()

        self.__check_missing(project, user, importer, h, g, utils)

        # Check when description is missing
        _json = {
            "forums": [{
                "shortname": "general",
                "name": "General Discussion",
                "threads": []
            }]
        }

        importer._load_json = mock.Mock(return_value=_json)
        self.__check_missing(project, user, importer, h, g, utils)
        utils.create_forum.assert_not_called()

        # Check when name is missing
        _json = {
            "forums": [{
                "shortname": "general",
                "description": "Description of forum",
                "threads": []
            }]
        }

        importer._load_json = mock.Mock(return_value=_json)
        self.__check_missing(project, user, importer, h, g, utils)
        utils.create_forum.assert_not_called()

        # Check when threads are missing
        _json = {
            "forums": [{
                "shortname": "general",
                "description": "Description of forum",
                "name": "General Discussion"
            }]
        }

        importer._load_json = mock.Mock(return_value=_json)
        self.__check_missing(project, user, importer, h, g, utils)

        # Check when subject of threads are missing
        _json = {
            "forums": [{
                "shortname": "general",
                "description": "Description of forum",
                "name": "General Discussion",
                "threads": [{
                    "posts": []
                }]
            }]
        }

        importer._load_json = mock.Mock(return_value=_json)
        self.__check_missing(project, user, importer, h, g, utils)

        # Check when posts of threads are missing
        _json = {
            "forums": [{
                "shortname": "general",
                "description": "Description of forum",
                "name": "General Discussion",
                "threads": [{
                    "subject": "Thread"
                }]
            }]
        }

        importer._load_json = mock.Mock(return_value=_json)
        self.__check_missing(project, user, importer, h, g, utils)

        self.assertEqual(h.make_app_admin_only.call_count, 6)
Esempio n. 9
0
    def test_import_tool_with_general_discussion(self, tlos, h, session, m, g,
                                                 c):
        """ This test creates a general discussion forum with one thread and one post from admin """

        importer = discussion.ForgeDiscussionImporter()
        _json = {
            'forums': [{
                'shortname':
                'general',
                '_id':
                '538f7240f697c62562ed9e6f',
                'description':
                'Forum about anything you want to talk about.',
                'name':
                'General Discussion',
                'threads': [{
                    'limit':
                    None,
                    'discussion_id':
                    '538f7240f697c62562ed9e6f',
                    '_id':
                    'ef260a33d2',
                    'page':
                    None,
                    'subject':
                    'Thread1',
                    'posts': [{
                        'attachments': [],
                        'author': 'admin1',
                        'timestamp': '2020-01-29 22:30:42.497000',
                        'author_icon_url': 'bla',
                        'text': 'hi',
                        'last_edited': None,
                        'slug': '00aa',
                        'subject': 'A post'
                    }]
                }]
            }]
        }
        importer._load_json = mock.Mock(return_value=_json)

        admin = mock.Mock(_id=1, is_anonymous=lambda: False)
        importer.get_user = mock.Mock(side_effect=[admin])

        project, user = mock.Mock(), mock.Mock()
        app = project.install_app.return_value
        app.config.options.mount_point = 'mount_point'
        app.config.options.import_id = {'source': 'Allura'}
        app.config.options.get = lambda *a: getattr(app.config.options, *a)
        app.url = 'foo'
        app.forums = []
        utils.create_forum = mock.Mock()
        forum = mock.Mock()
        thread1 = mock.Mock()
        forum.get_discussion_thread.return_value = (thread1, mock.Mock())
        utils.create_forum.return_value = forum
        importer.add_posts = mock.Mock()
        g.post_event = mock.Mock()
        m.AuditLog.log = mock.Mock()

        importer.import_tool(project,
                             user,
                             mount_point='mount_point',
                             mount_label='mount_label')

        project.install_app.assert_called_once_with(
            'discussion',
            'mount_point',
            'mount_label',
            import_id={'source': 'Allura'})

        h.push_config.assert_called_once_with(c, app=app)

        forum_json = _json['forums'][0]
        new_forum = self.__get_forum_dict(app, forum_json)

        utils.create_forum.assert_called_once_with(app, new_forum=new_forum)
        forum.get_discussion_thread(
            dict(headers=dict(
                Subject=_json["forums"][0]["threads"][0]["subject"])))
        importer.add_posts.assert_called_once_with(
            thread1, _json["forums"][0]["threads"][0]["posts"], app)

        m.AuditLog.log.assert_called_once_with(
            "import tool mount_point from exported Allura JSON",
            project=project,
            user=user,
            url='foo')
        g.post_event.assert_called_once_with("project_updated")
Esempio n. 10
0
    def test_import_tool_multiple_threads(self, tlos, h, session, m, g, c):
        """ This method tests if import_tool can handle multiple threads """

        importer = discussion.ForgeDiscussionImporter()

        _json = {
            "forums": [{
                "description":
                "Forum about anything you want to talk about.",
                "shortname":
                "general",
                "name":
                "General Discussion",
                "threads": [{
                    "page": None,
                    "subject": "Thread0",
                    "posts": []
                }, {
                    "page": None,
                    "subject": "Thread1",
                    "posts": []
                }, {
                    "page": None,
                    "subject": "Thread2",
                    "posts": []
                }, {
                    "page": None,
                    "subject": "Thread3",
                    "posts": []
                }, {
                    "page": None,
                    "subject": "Thread4",
                    "posts": []
                }]
            }]
        }

        importer._load_json = mock.Mock(return_value=_json)
        importer.add_posts = mock.Mock()
        importer._clear_forums = mock.Mock()

        project, user = mock.Mock(), mock.Mock()
        app = project.install_app.return_value
        app.config.options.mount_point = 'mount_point'
        app.config.options.import_id = {'source': 'Allura'}
        app.config.options.get = lambda *a: getattr(app.config.options, *a)
        app.url = 'foo'
        app.forums = []

        utils.create_forum = mock.Mock()
        forum = mock.Mock()
        forum.get_discussion_thread.return_value = (mock.Mock(), mock.Mock())

        utils.create_forum.return_value = forum

        g.post_event = mock.Mock()
        m.AuditLog.log = mock.Mock()

        importer.import_tool(project, user, 'mount_point', 'mount_label')

        project.install_app.assert_called_once_with(
            'discussion',
            'mount_point',
            'mount_label',
            import_id={'source': 'Allura'})
        h.push_config.assert_called_once_with(c, app=app)

        utils.create_forum.assert_called_once()

        thread_json = _json["forums"][0]["threads"]
        forum.get_discussion_thread.assert_has_calls([
            mock.call(dict(headers=dict(Subject=thread_json[0]["subject"]))),
            mock.call(dict(headers=dict(Subject=thread_json[1]["subject"]))),
            mock.call(dict(headers=dict(Subject=thread_json[2]["subject"]))),
            mock.call(dict(headers=dict(Subject=thread_json[3]["subject"]))),
            mock.call(dict(headers=dict(Subject=thread_json[4]["subject"])))
        ])

        self.assertEqual(importer.add_posts.call_count, 5)
        m.AuditLog.log.assert_called_once()
        g.post_event.assert_called_once()
Esempio n. 11
0
    def test_import_tool_multiple_forums(self, tlos, h, session, m, g, c):
        """ This test tests if it is possible to create a discussion with multiple forums """

        importer = discussion.ForgeDiscussionImporter()

        _json = {
            "forums": [{
                "shortname": "general",
                "description": "Forum about anything you want to talk about.",
                "name": "General Discussion",
                "threads": []
            }, {
                "shortname": "dev",
                "description": "Forum for Devs.",
                "name": "Developers' Corner",
                "threads": []
            }, {
                "shortname": "announcements",
                "description": "Announcements are posted here",
                "name": "Announcements",
                "threads": []
            }, {
                "shortname": "cc",
                "description": "Forum about improvements of our codebase",
                "name": "Clean Code",
                "threads": []
            }]
        }

        importer._load_json = mock.Mock(return_value=_json)
        importer.add_posts = mock.Mock()
        importer._clear_forums = mock.Mock()

        project, user = mock.Mock(), mock.Mock()
        app = project.install_app.return_value
        app.config.options.mount_point = 'mount_point'
        app.config.options.import_id = {'source': 'Allura'}
        app.config.options.get = lambda *a: getattr(app.config.options, *a)
        app.url = 'foo'
        app.forums = []

        utils.create_forum = mock.Mock()
        forum = mock.Mock()
        utils.create_forum.return_value = forum

        g.post_event = mock.Mock()
        m.AuditLog.log = mock.Mock()

        importer.import_tool(project, user, 'mount_point', 'mount_label')

        h.push_config.assert_called_once_with(c, app=app)
        importer._clear_forums.assert_called_once_with(app)

        forum0 = self.__get_forum_dict(app, _json['forums'][0])
        forum1 = self.__get_forum_dict(app, _json['forums'][1])
        forum2 = self.__get_forum_dict(app, _json['forums'][2])
        forum3 = self.__get_forum_dict(app, _json['forums'][3])

        utils.create_forum.assert_has_calls([
            mock.call(app, new_forum=forum0),
            mock.call(app, new_forum=forum1),
            mock.call(app, new_forum=forum2),
            mock.call(app, new_forum=forum3)
        ])

        forum.get_discussion_thread.assert_not_called()
        importer.add_posts.assert_not_called()

        m.AuditLog.log.assert_called_once()
        g.post_event.assert_called_once()
Esempio n. 12
0
    def test_import_tool_unicode(self, tlos, h, session, m, g, c):
        """ This method tests if the import tool method supports unicode characters """

        importer = discussion.ForgeDiscussionImporter()

        # Creating the json to test for
        _json = {
            "forums": [{
                "shortname":
                "general",
                "_id":
                "538f7240f697c62562ed9e6f",
                "description":
                "Forum about anything you want to talk about.",
                "name":
                "General Discussion",
                "threads": [{
                    "limit":
                    None,
                    "discussion_id":
                    "538f7240f697c62562ed9e6f",
                    "_id":
                    "88983164e3",
                    "posts": [{
                        "attachments": [],
                        "author":
                        "admin1",
                        "timestamp":
                        "2020-01-29 22:32:00.584000",
                        "author_icon_url":
                        "bla",
                        "text":
                        "test with un\u00ef\u00e7\u00f8\u2202\u00e9 text",
                        "last_edited":
                        None,
                        "slug":
                        "0e0e",
                        "subject":
                        "post with un\u00ef\u00e7\u00f8\u2202\u00e9"
                    }],
                    "page":
                    None,
                    "subject":
                    "post with un\u00ef\u00e7\u00f8\u2202\u00e9"
                }]
            }]
        }

        importer._load_json = mock.Mock(return_value=_json)

        project, user = mock.Mock(), mock.Mock()
        app = project.install_app.return_value
        app.config.options.mount_point = 'mount_point'
        app.config.options.import_id = {'source': 'Allura'}
        app.config.options.get = lambda *a: getattr(app.config.options, *a)
        app.url = 'foo'
        app.forums = []
        utils.create_forum = mock.Mock()
        forum = mock.Mock()
        thread1 = mock.Mock()
        p = mock.Mock()
        thread1.add_post = mock.Mock(return_value=p)
        forum.get_discussion_thread.return_value = (thread1, mock.Mock())
        utils.create_forum.return_value = forum
        g.post_event = mock.Mock()
        m.AuditLog.log = mock.Mock()
        admin = mock.Mock(is_anonymous=lambda: False)
        admin._id = 1
        importer.get_user = mock.Mock(return_value=admin)
        importer.annotate_text = mock.Mock(return_value='aa')

        importer.import_tool(project,
                             user,
                             mount_point='mount_point',
                             mount_label='mount_label')

        project.install_app.assert_called_once_with(
            'discussion',
            'mount_point',
            'mount_label',
            import_id={'source': 'Allura'})

        forum_json = _json["forums"][0]
        new_forum = self.__get_forum_dict(app, forum_json)

        utils.create_forum.assert_called_once_with(app, new_forum=new_forum)
        forum.get_discussion_thread.assert_called_once_with(
            dict(headers=dict(
                Subject=_json["forums"][0]["threads"][0]["subject"])))

        importer.get_user.assert_called_once_with('admin1')

        self.assertEqual(
            h.push_config.call_args_list,
            [mock.call(c, app=app),
             mock.call(c, user=admin, app=app)])

        post = _json["forums"][0]["threads"][0]["posts"][0]
        thread1.add_post.assert_called_once_with(subject=post["subject"],
                                                 text='aa',
                                                 timestamp=parse(
                                                     post["timestamp"]),
                                                 ignore_security=True,
                                                 parent_id=None)
        importer.annotate_text.assert_called_once_with(post["text"], admin,
                                                       post["author"])
        p.add_multiple_attachments.assert_called_once_with([])

        g.post_event.assert_called_once_with('project_updated')