From 344520c42487a81052c5cba6fd90d8dadfdade01 Mon Sep 17 00:00:00 2001 From: Jimmy Berry Date: Wed, 3 Jan 2018 14:21:24 -0600 Subject: [PATCH 1/9] tests/comment_tests: add test_add_marker(). --- tests/comment_tests.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/comment_tests.py b/tests/comment_tests.py index c13581bc..5df6d534 100644 --- a/tests/comment_tests.py +++ b/tests/comment_tests.py @@ -4,8 +4,16 @@ import unittest class TestComment(unittest.TestCase): + COMMENT = 'short comment' + COMMENT_INFO = {'foo': 'bar', 'distro': 'openSUSE'} + def setUp(self): self.api = CommentAPI('bogus') + self.bot = type(self).__name__ + self.comments = { + 1: {'comment': '\n\nshort comment'.format(self.bot)}, + 2: {'comment': '\n\nshort comment'.format(self.bot)} + } def test_truncate(self): comment = "string of text" @@ -46,3 +54,10 @@ handle tag_count = truncated.count('
') + truncated.count('
') self.assertEqual(tag_count, truncated.count('<')) self.assertEqual(tag_count, truncated.count('>')) + + def test_add_marker(self): + comment_marked = self.api.add_marker(self.COMMENT, self.bot) + self.assertEqual(comment_marked, self.comments[1]['comment']) + + comment_marked = self.api.add_marker(self.COMMENT, self.bot, self.COMMENT_INFO) + self.assertEqual(comment_marked, self.comments[2]['comment']) From 2bedaf61f4c026270d67390313dee20d9e27354b Mon Sep 17 00:00:00 2001 From: Jimmy Berry Date: Wed, 3 Jan 2018 14:32:29 -0600 Subject: [PATCH 2/9] tests/comment_tests: add test_remove_marker(). --- tests/comment_tests.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/comment_tests.py b/tests/comment_tests.py index 5df6d534..e12e3b44 100644 --- a/tests/comment_tests.py +++ b/tests/comment_tests.py @@ -61,3 +61,13 @@ handle comment_marked = self.api.add_marker(self.COMMENT, self.bot, self.COMMENT_INFO) self.assertEqual(comment_marked, self.comments[2]['comment']) + + def test_remove_marker(self): + comment = self.api.remove_marker(self.COMMENT) + self.assertEqual(comment, self.COMMENT) + + comment = self.api.remove_marker(self.comments[1]['comment']) + self.assertEqual(comment, self.COMMENT) + + comment = self.api.remove_marker(self.comments[2]['comment']) + self.assertEqual(comment, self.COMMENT) From 7778d056d771ffe6bdcfd566dae1a38cdf53a031 Mon Sep 17 00:00:00 2001 From: Jimmy Berry Date: Wed, 3 Jan 2018 14:34:05 -0600 Subject: [PATCH 3/9] tests/comment_tests: add test_comment_find(). --- tests/comment_tests.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/comment_tests.py b/tests/comment_tests.py index e12e3b44..7d72b20f 100644 --- a/tests/comment_tests.py +++ b/tests/comment_tests.py @@ -71,3 +71,15 @@ handle comment = self.api.remove_marker(self.comments[2]['comment']) self.assertEqual(comment, self.COMMENT) + + def test_comment_find(self): + comment, info = self.api.comment_find(self.comments, self.bot) + self.assertEqual(comment, self.comments[1]) + + comment, info = self.api.comment_find(self.comments, self.bot, self.COMMENT_INFO) + self.assertEqual(comment, self.comments[2]) + + info_partial = self.COMMENT_INFO + del info_partial['foo'] + comment, info = self.api.comment_find(self.comments, self.bot, info_partial) + self.assertEqual(comment, self.comments[2]) From f1462def6e7cca13483b31a30c9ded68f1d9d18e Mon Sep 17 00:00:00 2001 From: Jimmy Berry Date: Wed, 3 Jan 2018 16:19:28 -0600 Subject: [PATCH 4/9] tests/OBSLocal: setup test separate cookiejar. --- tests/OBSLocal.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/OBSLocal.py b/tests/OBSLocal.py index 63f602c0..5199561d 100644 --- a/tests/OBSLocal.py +++ b/tests/OBSLocal.py @@ -8,6 +8,7 @@ import subprocess import unittest OSCRC = os.path.expanduser('~/.oscrc-test') +OSCCOOKIEJAR = os.path.expanduser('~/.osc_cookiejar-test') APIURL = 'local-test' class OBSLocalTestCase(unittest.TestCase): @@ -23,6 +24,10 @@ class OBSLocalTestCase(unittest.TestCase): httpretty.disable() def setUp(self): + if os.path.exists(OSCCOOKIEJAR): + # Avoid stale cookiejar since local OBS may be completely reset. + os.remove(OSCCOOKIEJAR) + self.oscrc('Admin') conf.get_config(override_conffile=OSCRC, override_no_keyring=True, @@ -41,6 +46,7 @@ class OBSLocalTestCase(unittest.TestCase): f.write('\n'.join([ '[general]', 'apiurl = http://0.0.0.0:3000', + 'cookiejar = {}'.format(OSCCOOKIEJAR), '[http://0.0.0.0:3000]', 'user = {}'.format(userid), 'pass = opensuse', From df8cd0f6779ef6d1d5d5b87d47fc1553f6b2f804 Mon Sep 17 00:00:00 2001 From: Jimmy Berry Date: Wed, 3 Jan 2018 16:22:13 -0600 Subject: [PATCH 5/9] tests/OBSLocal: re-parse config and reset authentication when changing user. Previously, the user was changed, but the authentication not reset. For osc.core calls made within the text context they would still run as Admin while separate processes (like scripts) would run as the desired user. As such this was not an issue before since only scripts were meant to run as a different user. --- tests/OBSLocal.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/tests/OBSLocal.py b/tests/OBSLocal.py index 5199561d..4a3e2c1d 100644 --- a/tests/OBSLocal.py +++ b/tests/OBSLocal.py @@ -28,10 +28,7 @@ class OBSLocalTestCase(unittest.TestCase): # Avoid stale cookiejar since local OBS may be completely reset. os.remove(OSCCOOKIEJAR) - self.oscrc('Admin') - conf.get_config(override_conffile=OSCRC, - override_no_keyring=True, - override_no_gnome_keyring=True) + self.osc_user('Admin') self.apiurl = conf.config['apiurl'] self.assertOBS() @@ -56,9 +53,24 @@ class OBSLocalTestCase(unittest.TestCase): ])) def osc_user(self, userid): - conf.config['api_host_options'][self.apiurl]['user'] = userid self.oscrc(userid) + # Rather than modify userid and email, just re-parse entire config and + # reset authentication by clearing opener to avoid edge-cases. + self.oscParse() + + def oscParse(self): + # Otherwise, will stick to first user for a given apiurl. + conf._build_opener.last_opener = (None, None) + + # Otherwise, will not re-parse same config file. + if 'cp' in conf.get_configParser.__dict__: + del conf.get_configParser.cp + + conf.get_config(override_conffile=OSCRC, + override_no_keyring=True, + override_no_gnome_keyring=True) + def execute_script(self, args): if self.script: args.insert(0, self.script) From 89ebe7e67d19b5869157e97714f800c823f8f4a5 Mon Sep 17 00:00:00 2001 From: Jimmy Berry Date: Wed, 3 Jan 2018 16:24:21 -0600 Subject: [PATCH 6/9] tests/comment_test: move COMMENT* constants to file level. --- tests/comment_tests.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/tests/comment_tests.py b/tests/comment_tests.py index 7d72b20f..923334bb 100644 --- a/tests/comment_tests.py +++ b/tests/comment_tests.py @@ -3,10 +3,10 @@ import re import unittest -class TestComment(unittest.TestCase): - COMMENT = 'short comment' - COMMENT_INFO = {'foo': 'bar', 'distro': 'openSUSE'} +COMMENT = 'short comment' +COMMENT_INFO = {'foo': 'bar', 'distro': 'openSUSE'} +class TestComment(unittest.TestCase): def setUp(self): self.api = CommentAPI('bogus') self.bot = type(self).__name__ @@ -56,30 +56,32 @@ handle self.assertEqual(tag_count, truncated.count('>')) def test_add_marker(self): - comment_marked = self.api.add_marker(self.COMMENT, self.bot) + comment_marked = self.api.add_marker(COMMENT, self.bot) self.assertEqual(comment_marked, self.comments[1]['comment']) - comment_marked = self.api.add_marker(self.COMMENT, self.bot, self.COMMENT_INFO) + comment_marked = self.api.add_marker(COMMENT, self.bot, COMMENT_INFO) self.assertEqual(comment_marked, self.comments[2]['comment']) def test_remove_marker(self): - comment = self.api.remove_marker(self.COMMENT) - self.assertEqual(comment, self.COMMENT) + comment = self.api.remove_marker(COMMENT) + self.assertEqual(comment, COMMENT) comment = self.api.remove_marker(self.comments[1]['comment']) - self.assertEqual(comment, self.COMMENT) + self.assertEqual(comment, COMMENT) comment = self.api.remove_marker(self.comments[2]['comment']) - self.assertEqual(comment, self.COMMENT) + self.assertEqual(comment, COMMENT) def test_comment_find(self): comment, info = self.api.comment_find(self.comments, self.bot) self.assertEqual(comment, self.comments[1]) - comment, info = self.api.comment_find(self.comments, self.bot, self.COMMENT_INFO) + comment, info = self.api.comment_find(self.comments, self.bot, COMMENT_INFO) self.assertEqual(comment, self.comments[2]) + self.assertEqual(info, COMMENT_INFO) - info_partial = self.COMMENT_INFO + info_partial = dict(COMMENT_INFO) del info_partial['foo'] comment, info = self.api.comment_find(self.comments, self.bot, info_partial) self.assertEqual(comment, self.comments[2]) + self.assertEqual(info, COMMENT_INFO) From f514b22828919088dfd033c084f29dc5e29f10b8 Mon Sep 17 00:00:00 2001 From: Jimmy Berry Date: Wed, 3 Jan 2018 16:24:59 -0600 Subject: [PATCH 7/9] tests/comment_test: add test_basic() against OBSLocal. --- tests/comment_tests.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/comment_tests.py b/tests/comment_tests.py index 923334bb..69508218 100644 --- a/tests/comment_tests.py +++ b/tests/comment_tests.py @@ -1,10 +1,13 @@ +from OBSLocal import OBSLocalTestCase from osclib.comments import CommentAPI +import random import re import unittest COMMENT = 'short comment' COMMENT_INFO = {'foo': 'bar', 'distro': 'openSUSE'} +PROJECT = 'openSUSE:Factory:Staging' class TestComment(unittest.TestCase): def setUp(self): @@ -85,3 +88,28 @@ handle comment, info = self.api.comment_find(self.comments, self.bot, info_partial) self.assertEqual(comment, self.comments[2]) self.assertEqual(info, COMMENT_INFO) + + +class TestCommentOBS(OBSLocalTestCase): + def setUp(self): + super(TestCommentOBS, self).setUp() + self.api = CommentAPI(self.apiurl) + # Ensure different test runs operate in unique namespace. + self.bot = '::'.join([type(self).__name__, str(random.getrandbits(8))]) + + def test_basic(self): + self.osc_user('staging-bot') + + self.assertFalse(self.comments_filtered(self.bot)[0]) + + self.assertTrue(self.api.add_comment( + project_name=PROJECT, comment=self.api.add_marker(COMMENT, self.bot))) + comment, _ = self.comments_filtered(self.bot) + self.assertTrue(comment) + + self.assertTrue(self.api.delete(comment['id'])) + self.assertFalse(self.comments_filtered(self.bot)[0]) + + def comments_filtered(self, bot): + comments = self.api.get_comments(project_name=PROJECT) + return self.api.comment_find(comments, bot) From 20f73f49b2e163c4a17aa4637fb397b4693a17e9 Mon Sep 17 00:00:00 2001 From: Jimmy Berry Date: Wed, 3 Jan 2018 16:25:43 -0600 Subject: [PATCH 8/9] tests/comment_test: add test_delete_batch(). --- tests/comment_tests.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/comment_tests.py b/tests/comment_tests.py index 69508218..42849532 100644 --- a/tests/comment_tests.py +++ b/tests/comment_tests.py @@ -110,6 +110,28 @@ class TestCommentOBS(OBSLocalTestCase): self.assertTrue(self.api.delete(comment['id'])) self.assertFalse(self.comments_filtered(self.bot)[0]) + def test_delete_batch(self): + users = ['factory-auto', 'repo-checker', 'staging-bot'] + for user in users: + self.osc_user(user) + from osc import conf + bot = '::'.join([self.bot, user]) + comment = self.api.add_marker(COMMENT, bot) + + self.assertFalse(self.comments_filtered(bot)[0]) + self.assertTrue(self.api.add_comment(project_name=PROJECT, comment=comment)) + self.assertTrue(self.comments_filtered(bot)[0]) + + # Allow for existing comments by basing assertion on delta from initial count. + comment_count = len(self.api.get_comments(project_name=PROJECT)) + self.assertTrue(comment_count >= len(users)) + + self.api.delete_from_where_user(users[0], project_name=PROJECT) + self.assertTrue(len(self.api.get_comments(project_name=PROJECT)) == comment_count - 1) + + self.api.delete_from(project_name=PROJECT) + self.assertFalse(len(self.api.get_comments(project_name=PROJECT))) + def comments_filtered(self, bot): comments = self.api.get_comments(project_name=PROJECT) return self.api.comment_find(comments, bot) From 23542327e174666231568daaab2d98ae9f76b253 Mon Sep 17 00:00:00 2001 From: Jimmy Berry Date: Wed, 3 Jan 2018 16:40:29 -0600 Subject: [PATCH 9/9] tests/comment_test: add test_delete_nested(). --- tests/comment_tests.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/comment_tests.py b/tests/comment_tests.py index 42849532..37f6b45f 100644 --- a/tests/comment_tests.py +++ b/tests/comment_tests.py @@ -110,6 +110,34 @@ class TestCommentOBS(OBSLocalTestCase): self.assertTrue(self.api.delete(comment['id'])) self.assertFalse(self.comments_filtered(self.bot)[0]) + def test_delete_nested(self): + self.osc_user('staging-bot') + comment_marked = self.api.add_marker(COMMENT, self.bot) + + # Allow for existing comments by basing assertion on delta from initial count. + comment_count = len(self.api.get_comments(project_name=PROJECT)) + self.assertFalse(self.comments_filtered(self.bot)[0]) + + self.assertTrue(self.api.add_comment(project_name=PROJECT, comment=comment_marked)) + comment, _ = self.comments_filtered(self.bot) + self.assertTrue(comment) + + for i in range(0, 3): + self.assertTrue(self.api.add_comment( + project_name=PROJECT, comment=comment_marked, parent_id=comment['id'])) + + comments = self.api.get_comments(project_name=PROJECT) + parented_count = 0 + for comment in comments.values(): + if comment['parent']: + parented_count += 1 + + self.assertEqual(parented_count, 3) + self.assertTrue(len(comments) == comment_count + 4) + + self.api.delete_from(project_name=PROJECT) + self.assertFalse(len(self.api.get_comments(project_name=PROJECT))) + def test_delete_batch(self): users = ['factory-auto', 'repo-checker', 'staging-bot'] for user in users: