76ba10820adf9631efb5b99532204896ca83a010 — Kridsada Thanabulpong 11 months ago 743a628
Expose scope service to banword service.
M fanboi2/services/__init__.py => fanboi2/services/__init__.py +1 -1
@@ 81,7 81,7 @@ SERVICES = (
     (IBanQueryService, BanQueryService, "db", IScopeService),
     (IBanUpdateService, BanUpdateService, "db"),
     (IBanwordCreateService, BanwordCreateService, "db"),
-    (IBanwordQueryService, BanwordQueryService, "db"),
+    (IBanwordQueryService, BanwordQueryService, "db", IScopeService),
     (IBanwordUpdateService, BanwordUpdateService, "db"),
     (IBoardCreateService, BoardCreateService, "db"),
     (IBoardQueryService, BoardQueryService, "db"),

M fanboi2/services/banword.py => fanboi2/services/banword.py +2 -1
@@ 32,8 32,9 @@ class BanwordCreateService(object):
 class BanwordQueryService(object):
     """Banword query service provides a service for querying banwords."""
 
-    def __init__(self, dbsession):
+    def __init__(self, dbsession, scope_svc):
         self.dbsession = dbsession
+        self.scope_svc = scope_svc
 
     def list_active(self):
         """Returns a list of banwords that are currently active."""

M fanboi2/tests/test_integrations_admin_banwords.py => fanboi2/tests/test_integrations_admin_banwords.py +29 -18
@@ 10,7 10,7 @@ class TestIntegrationAdminBanwords(IntegrationMixin, unittest.TestCase):
     def test_banwords_get(self):
         from ..interfaces import IBanwordQueryService
         from ..models import Banword
-        from ..services import BanwordQueryService
+        from ..services import BanwordQueryService, ScopeService
         from ..views.admin import banwords_get
         from . import mock_service
 


@@ 19,7 19,8 @@ class TestIntegrationAdminBanwords(IntegrationMixin, unittest.TestCase):
         self._make(Banword(expr="https?:\\/\\/youtu\\.be", active=False))
         self._make(Banword(expr="https?:\\/\\/example\\.com", active=False))
         request = mock_service(
-            self.request, {IBanwordQueryService: BanwordQueryService(self.dbsession)}
+            self.request,
+            {IBanwordQueryService: BanwordQueryService(self.dbsession, ScopeService())},
         )
         request.method = "GET"
         response = banwords_get(request)


@@ 28,7 29,7 @@ class TestIntegrationAdminBanwords(IntegrationMixin, unittest.TestCase):
     def test_banwords_inactive_get(self):
         from ..interfaces import IBanwordQueryService
         from ..models import Banword
-        from ..services import BanwordQueryService
+        from ..services import BanwordQueryService, ScopeService
         from ..views.admin import banwords_inactive_get
         from . import mock_service
 


@@ 38,7 39,8 @@ class TestIntegrationAdminBanwords(IntegrationMixin, unittest.TestCase):
         banword4 = self._make(Banword(expr="https?:\\/\\/example\\.com", active=False))
         self.dbsession.commit()
         request = mock_service(
-            self.request, {IBanwordQueryService: BanwordQueryService(self.dbsession)}
+            self.request,
+            {IBanwordQueryService: BanwordQueryService(self.dbsession, ScopeService())},
         )
         request.method = "GET"
         response = banwords_inactive_get(request)


@@ 124,14 126,15 @@ class TestIntegrationAdminBanwords(IntegrationMixin, unittest.TestCase):
     def test_banword_get(self):
         from ..models import Banword
         from ..interfaces import IBanwordQueryService
-        from ..services import BanwordQueryService
+        from ..services import BanwordQueryService, ScopeService
         from ..views.admin import banword_get
         from . import mock_service
 
         banword = self._make(Banword(expr="https?:\\/\\/bit\\.ly"))
         self.dbsession.commit()
         request = mock_service(
-            self.request, {IBanwordQueryService: BanwordQueryService(self.dbsession)}
+            self.request,
+            {IBanwordQueryService: BanwordQueryService(self.dbsession, ScopeService())},
         )
         request.method = "GET"
         request.matchdict["banword"] = str(banword.id)


@@ 141,12 144,13 @@ class TestIntegrationAdminBanwords(IntegrationMixin, unittest.TestCase):
     def test_banword_get_not_found(self):
         from sqlalchemy.orm.exc import NoResultFound
         from ..interfaces import IBanwordQueryService
-        from ..services import BanwordQueryService
+        from ..services import BanwordQueryService, ScopeService
         from ..views.admin import banword_get
         from . import mock_service
 
         request = mock_service(
-            self.request, {IBanwordQueryService: BanwordQueryService(self.dbsession)}
+            self.request,
+            {IBanwordQueryService: BanwordQueryService(self.dbsession, ScopeService())},
         )
         request.method = "GET"
         request.matchdict["banword"] = "-1"


@@ 156,7 160,7 @@ class TestIntegrationAdminBanwords(IntegrationMixin, unittest.TestCase):
     def test_banword_edit_get(self):
         from ..models import Banword
         from ..interfaces import IBanwordQueryService
-        from ..services import BanwordQueryService
+        from ..services import BanwordQueryService, ScopeService
         from ..views.admin import banword_edit_get
         from . import mock_service
 


@@ 167,7 171,8 @@ class TestIntegrationAdminBanwords(IntegrationMixin, unittest.TestCase):
         )
         self.dbsession.commit()
         request = mock_service(
-            self.request, {IBanwordQueryService: BanwordQueryService(self.dbsession)}
+            self.request,
+            {IBanwordQueryService: BanwordQueryService(self.dbsession, ScopeService())},
         )
         request.method = "GET"
         request.matchdict["banword"] = str(banword.id)


@@ 180,12 185,13 @@ class TestIntegrationAdminBanwords(IntegrationMixin, unittest.TestCase):
     def test_banword_edit_get_not_found(self):
         from sqlalchemy.orm.exc import NoResultFound
         from ..interfaces import IBanwordQueryService
-        from ..services import BanwordQueryService
+        from ..services import BanwordQueryService, ScopeService
         from ..views.admin import banword_edit_get
         from . import mock_service
 
         request = mock_service(
-            self.request, {IBanwordQueryService: BanwordQueryService(self.dbsession)}
+            self.request,
+            {IBanwordQueryService: BanwordQueryService(self.dbsession, ScopeService())},
         )
         request.method = "GET"
         request.matchdict["banword"] = "-1"


@@ 195,7 201,7 @@ class TestIntegrationAdminBanwords(IntegrationMixin, unittest.TestCase):
     def test_banword_edit_post(self):
         from ..models import Banword
         from ..interfaces import IBanwordQueryService, IBanwordUpdateService
-        from ..services import BanwordQueryService, BanwordUpdateService
+        from ..services import BanwordQueryService, BanwordUpdateService, ScopeService
         from ..views.admin import banword_edit_post
         from . import mock_service
 


@@ 208,7 214,9 @@ class TestIntegrationAdminBanwords(IntegrationMixin, unittest.TestCase):
         request = mock_service(
             self.request,
             {
-                IBanwordQueryService: BanwordQueryService(self.dbsession),
+                IBanwordQueryService: BanwordQueryService(
+                    self.dbsession, ScopeService()
+                ),
                 IBanwordUpdateService: BanwordUpdateService(self.dbsession),
             },
         )


@@ 230,12 238,13 @@ class TestIntegrationAdminBanwords(IntegrationMixin, unittest.TestCase):
     def test_banword_edit_post_not_found(self):
         from sqlalchemy.orm.exc import NoResultFound
         from ..interfaces import IBanwordQueryService
-        from ..services import BanwordQueryService
+        from ..services import BanwordQueryService, ScopeService
         from ..views.admin import banword_edit_post
         from . import mock_service
 
         request = mock_service(
-            self.request, {IBanwordQueryService: BanwordQueryService(self.dbsession)}
+            self.request,
+            {IBanwordQueryService: BanwordQueryService(self.dbsession, ScopeService())},
         )
         request.method = "POST"
         request.matchdict["banword"] = "-1"


@@ 264,7 273,7 @@ class TestIntegrationAdminBanwords(IntegrationMixin, unittest.TestCase):
     def test_banword_edit_post_invalid_banword(self):
         from ..models import Banword
         from ..interfaces import IBanwordQueryService, IBanwordUpdateService
-        from ..services import BanwordQueryService, BanwordUpdateService
+        from ..services import BanwordQueryService, BanwordUpdateService, ScopeService
         from ..views.admin import banword_edit_post
         from . import mock_service
 


@@ 277,7 286,9 @@ class TestIntegrationAdminBanwords(IntegrationMixin, unittest.TestCase):
         request = mock_service(
             self.request,
             {
-                IBanwordQueryService: BanwordQueryService(self.dbsession),
+                IBanwordQueryService: BanwordQueryService(
+                    self.dbsession, ScopeService()
+                ),
                 IBanwordUpdateService: BanwordUpdateService(self.dbsession),
             },
         )

M fanboi2/tests/test_integrations_api.py => fanboi2/tests/test_integrations_api.py +24 -8
@@ 243,7 243,9 @@ class TestIntegrationAPI(ModelSessionMixin, unittest.TestCase):
                 IBoardQueryService: BoardQueryService(self.dbsession),
                 IRateLimiterService: rate_limiter_svc,
                 IBanQueryService: BanQueryService(self.dbsession, ScopeService()),
-                IBanwordQueryService: BanwordQueryService(self.dbsession),
+                IBanwordQueryService: BanwordQueryService(
+                    self.dbsession, ScopeService()
+                ),
                 ITopicCreateService: TopicCreateService(
                     self.dbsession,
                     IdentityService(


@@ 323,7 325,9 @@ class TestIntegrationAPI(ModelSessionMixin, unittest.TestCase):
                 IBoardQueryService: BoardQueryService(self.dbsession),
                 IRateLimiterService: rate_limiter_svc,
                 IBanQueryService: BanQueryService(self.dbsession, ScopeService()),
-                IBanwordQueryService: BanwordQueryService(self.dbsession),
+                IBanwordQueryService: BanwordQueryService(
+                    self.dbsession, ScopeService()
+                ),
                 ITopicCreateService: TopicCreateService(
                     self.dbsession,
                     IdentityService(


@@ 511,7 515,9 @@ class TestIntegrationAPI(ModelSessionMixin, unittest.TestCase):
             self.request,
             {
                 IBanQueryService: BanQueryService(self.dbsession, ScopeService()),
-                IBanwordQueryService: BanwordQueryService(self.dbsession),
+                IBanwordQueryService: BanwordQueryService(
+                    self.dbsession, ScopeService()
+                ),
                 IBoardQueryService: BoardQueryService(self.dbsession),
             },
         )


@@ 555,7 561,9 @@ class TestIntegrationAPI(ModelSessionMixin, unittest.TestCase):
                 IBoardQueryService: BoardQueryService(self.dbsession),
                 IRateLimiterService: rate_limiter_svc,
                 IBanQueryService: BanQueryService(self.dbsession, ScopeService()),
-                IBanwordQueryService: BanwordQueryService(self.dbsession),
+                IBanwordQueryService: BanwordQueryService(
+                    self.dbsession, ScopeService()
+                ),
             },
         )
         request.method = "POST"


@@ 818,7 826,9 @@ class TestIntegrationAPI(ModelSessionMixin, unittest.TestCase):
                 ),
                 IRateLimiterService: rate_limiter_svc,
                 IBanQueryService: BanQueryService(self.dbsession, ScopeService()),
-                IBanwordQueryService: BanwordQueryService(self.dbsession),
+                IBanwordQueryService: BanwordQueryService(
+                    self.dbsession, ScopeService()
+                ),
                 IPostCreateService: PostCreateService(
                     self.dbsession,
                     IdentityService(


@@ 903,7 913,9 @@ class TestIntegrationAPI(ModelSessionMixin, unittest.TestCase):
                 ),
                 IRateLimiterService: rate_limiter_svc,
                 IBanQueryService: BanQueryService(self.dbsession, ScopeService()),
-                IBanwordQueryService: BanwordQueryService(self.dbsession),
+                IBanwordQueryService: BanwordQueryService(
+                    self.dbsession, ScopeService()
+                ),
                 IPostCreateService: PostCreateService(
                     self.dbsession,
                     IdentityService(


@@ 1104,7 1116,9 @@ class TestIntegrationAPI(ModelSessionMixin, unittest.TestCase):
                     self.dbsession, BoardQueryService(self.dbsession)
                 ),
                 IBanQueryService: BanQueryService(self.dbsession, ScopeService()),
-                IBanwordQueryService: BanwordQueryService(self.dbsession),
+                IBanwordQueryService: BanwordQueryService(
+                    self.dbsession, ScopeService()
+                ),
             },
         )
 


@@ 1153,7 1167,9 @@ class TestIntegrationAPI(ModelSessionMixin, unittest.TestCase):
                 ),
                 IRateLimiterService: rate_limiter_svc,
                 IBanQueryService: BanQueryService(self.dbsession, ScopeService()),
-                IBanwordQueryService: BanwordQueryService(self.dbsession),
+                IBanwordQueryService: BanwordQueryService(
+                    self.dbsession, ScopeService()
+                ),
             },
         )
         request.method = "POST"

M fanboi2/tests/test_integrations_board.py => fanboi2/tests/test_integrations_board.py +18 -6
@@ 585,7 585,9 @@ class TestIntegrationBoard(IntegrationMixin, unittest.TestCase):
                 IBoardQueryService: BoardQueryService(self.dbsession),
                 IRateLimiterService: rate_limiter_svc,
                 IBanQueryService: BanQueryService(self.dbsession, ScopeService()),
-                IBanwordQueryService: BanwordQueryService(self.dbsession),
+                IBanwordQueryService: BanwordQueryService(
+                    self.dbsession, ScopeService()
+                ),
                 ITopicCreateService: TopicCreateService(
                     self.dbsession,
                     IdentityService(


@@ 818,7 820,9 @@ class TestIntegrationBoard(IntegrationMixin, unittest.TestCase):
             {
                 IBoardQueryService: BoardQueryService(self.dbsession),
                 IBanQueryService: BanQueryService(self.dbsession, ScopeService()),
-                IBanwordQueryService: BanwordQueryService(self.dbsession),
+                IBanwordQueryService: BanwordQueryService(
+                    self.dbsession, ScopeService()
+                ),
             },
         )
         request.method = "POST"


@@ 863,7 867,9 @@ class TestIntegrationBoard(IntegrationMixin, unittest.TestCase):
                 IBoardQueryService: BoardQueryService(self.dbsession),
                 IRateLimiterService: rate_limiter_svc,
                 IBanQueryService: BanQueryService(self.dbsession, ScopeService()),
-                IBanwordQueryService: BanwordQueryService(self.dbsession),
+                IBanwordQueryService: BanwordQueryService(
+                    self.dbsession, ScopeService()
+                ),
             },
         )
         request.method = "POST"


@@ 1491,7 1497,9 @@ class TestIntegrationBoard(IntegrationMixin, unittest.TestCase):
                 ),
                 IRateLimiterService: rate_limiter_svc,
                 IBanQueryService: BanQueryService(self.dbsession, ScopeService()),
-                IBanwordQueryService: BanwordQueryService(self.dbsession),
+                IBanwordQueryService: BanwordQueryService(
+                    self.dbsession, ScopeService()
+                ),
                 IPostCreateService: PostCreateService(
                     self.dbsession,
                     IdentityService(


@@ 1820,7 1828,9 @@ class TestIntegrationBoard(IntegrationMixin, unittest.TestCase):
                     self.dbsession, BoardQueryService(self.dbsession)
                 ),
                 IBanQueryService: BanQueryService(self.dbsession, ScopeService()),
-                IBanwordQueryService: BanwordQueryService(self.dbsession),
+                IBanwordQueryService: BanwordQueryService(
+                    self.dbsession, ScopeService()
+                ),
             },
         )
         request.method = "POST"


@@ 1875,7 1885,9 @@ class TestIntegrationBoard(IntegrationMixin, unittest.TestCase):
                 ),
                 IRateLimiterService: rate_limiter_svc,
                 IBanQueryService: BanQueryService(self.dbsession, ScopeService()),
-                IBanwordQueryService: BanwordQueryService(self.dbsession),
+                IBanwordQueryService: BanwordQueryService(
+                    self.dbsession, ScopeService()
+                ),
             },
         )
         request.method = "POST"

M fanboi2/tests/test_services_banword.py => fanboi2/tests/test_services_banword.py +39 -26
@@ 13,15 13,15 @@ class TestBanwordCreateService(ModelSessionMixin, unittest.TestCase):
     def test_create(self):
         banword_create_svc = self._get_target_class()(self.dbsession)
         banword = banword_create_svc.create(
-            "https?:\/\/bit\.ly", description="no shortlinks", active=True
+            r"https?:\/\/bit\.ly", description="no shortlinks", active=True
         )
-        self.assertEqual(banword.expr, "https?:\/\/bit\.ly")
+        self.assertEqual(banword.expr, r"https?:\/\/bit\.ly")
         self.assertEqual(banword.description, "no shortlinks")
         self.assertTrue(banword.active)
 
     def test_create_without_optional_fields(self):
         banword_create_svc = self._get_target_class()(self.dbsession)
-        banword = banword_create_svc.create("https?:\/\/bit\.ly")
+        banword = banword_create_svc.create(r"https?:\/\/bit\.ly")
         self.assertIsNone(banword.description)
         self.assertTrue(banword.active)
 


@@ 34,7 34,7 @@ class TestBanwordCreateService(ModelSessionMixin, unittest.TestCase):
 
     def test_create_deactivated(self):
         banword_create_svc = self._get_target_class()(self.dbsession)
-        banword = banword_create_svc.create("https?:\/\/bit\.ly", active=False)
+        banword = banword_create_svc.create(r"https?:\/\/bit\.ly", active=False)
         self.assertFalse(banword.active)
 
 


@@ 44,37 44,44 @@ class TestBanwordQueryService(ModelSessionMixin, unittest.TestCase):
 
         return BanwordQueryService
 
+    def _make_one(self, retval=True):
+        class _DummyScopeService(object):
+            def evaluate(self, _scope, _obj):
+                return retval
+
+        return self._get_target_class()(self.dbsession, _DummyScopeService())
+
     def test_list_active(self):
         from ..models import Banword
 
-        banword1 = self._make(Banword(expr="https?:\/\/bit\.ly"))
-        banword2 = self._make(Banword(expr="https?:\/\/goo\.gl"))
-        self._make(Banword(expr="https?:\/\/youtu\.be", active=False))
-        self._make(Banword(expr="https?:\/\/example\.com", active=False))
+        banword1 = self._make(Banword(expr=r"https?:\/\/bit\.ly"))
+        banword2 = self._make(Banword(expr=r"https?:\/\/goo\.gl"))
+        self._make(Banword(expr=r"https?:\/\/youtu\.be", active=False))
+        self._make(Banword(expr=r"https?:\/\/example\.com", active=False))
         self.dbsession.commit()
-        banword_query_svc = self._get_target_class()(self.dbsession)
+        banword_query_svc = self._make_one()
         self.assertEqual(banword_query_svc.list_active(), [banword2, banword1])
 
     def test_list_inactive(self):
         from ..models import Banword
 
-        self._make(Banword(expr="https?:\/\/bit\.ly"))
-        self._make(Banword(expr="https?:\/\/goo\.gl"))
-        banword3 = self._make(Banword(expr="https?:\/\/youtu\.be", active=False))
-        banword4 = self._make(Banword(expr="https?:\/\/example\.com", active=False))
+        self._make(Banword(expr=r"https?:\/\/bit\.ly"))
+        self._make(Banword(expr=r"https?:\/\/goo\.gl"))
+        banword3 = self._make(Banword(expr=r"https?:\/\/youtu\.be", active=False))
+        banword4 = self._make(Banword(expr=r"https?:\/\/example\.com", active=False))
         self.dbsession.commit()
-        banword_query_svc = self._get_target_class()(self.dbsession)
+        banword_query_svc = self._make_one()
         self.assertEqual(banword_query_svc.list_inactive(), [banword4, banword3])
 
     def test_is_banned(self):
         from ..models import Banword
 
-        self._make(Banword(expr="https?:\/\/bit\.ly"))
-        self._make(Banword(expr="https?:\/\/goo\.gl"))
-        self._make(Banword(expr="https?:\/\/youtu\.be", active=False))
-        self._make(Banword(expr="https?:\/\/example\.com", active=False))
+        self._make(Banword(expr=r"https?:\/\/bit\.ly"))
+        self._make(Banword(expr=r"https?:\/\/goo\.gl"))
+        self._make(Banword(expr=r"https?:\/\/youtu\.be", active=False))
+        self._make(Banword(expr=r"https?:\/\/example\.com", active=False))
         self.dbsession.commit()
-        banword_query_svc = self._get_target_class()(self.dbsession)
+        banword_query_svc = self._make_one()
         self.assertTrue(banword_query_svc.is_banned("a\nb\nhttps://bit.ly/Spam\nd"))
         self.assertTrue(banword_query_svc.is_banned("a\nb\nhttps://goo.gl/Spam\nd"))
         self.assertFalse(banword_query_svc.is_banned("a\nb\nhttps://youtu.be/Spam\nd"))


@@ 85,9 92,9 @@ class TestBanwordQueryService(ModelSessionMixin, unittest.TestCase):
     def test_banword_from_id(self):
         from ..models import Banword
 
-        banword = self._make(Banword(expr="https?:\/\/bit\.ly"))
+        banword = self._make(Banword(expr=r"https?:\/\/bit\.ly"))
         self.dbsession.commit()
-        banword_query_svc = self._get_target_class()(self.dbsession)
+        banword_query_svc = self._make_one()
         self.assertEqual(banword_query_svc.banword_from_id(banword.id), banword)
 
 


@@ 101,17 108,19 @@ class TestBanwordUpdateService(ModelSessionMixin, unittest.TestCase):
         from ..models import Banword
 
         banword = self._make(
-            Banword(expr="https?:\/\/bit\.ly", description="no shortlinks", active=True)
+            Banword(
+                expr=r"https?:\/\/bit\.ly", description="no shortlinks", active=True
+            )
         )
         self.dbsession.commit()
         banword_update_svc = self._get_target_class()(self.dbsession)
         banword = banword_update_svc.update(
             banword.id,
-            expr="https?:\/\/(bit\.ly|goo\.gl)",
+            expr=r"https?:\/\/(bit\.ly|goo\.gl)",
             description="no any shortlinks",
             active=False,
         )
-        self.assertEqual(banword.expr, "https?:\/\/(bit\.ly|goo\.gl)")
+        self.assertEqual(banword.expr, r"https?:\/\/(bit\.ly|goo\.gl)")
         self.assertEqual(banword.description, "no any shortlinks")
         self.assertFalse(banword.active)
 


@@ 126,7 135,9 @@ class TestBanwordUpdateService(ModelSessionMixin, unittest.TestCase):
         from ..models import Banword
 
         banword = self._make(
-            Banword(expr="https?:\/\/bit\.ly", description="no shortlinks", active=True)
+            Banword(
+                expr=r"https?:\/\/bit\.ly", description="no shortlinks", active=True
+            )
         )
         self.dbsession.commit()
         banword_update_svc = self._get_target_class()(self.dbsession)


@@ 141,7 152,9 @@ class TestBanwordUpdateService(ModelSessionMixin, unittest.TestCase):
         from ..models import Banword
 
         banword = self._make(
-            Banword(expr="https?:\/\/bit\.ly", description="no shortlinks", active=True)
+            Banword(
+                expr=r"https?:\/\/bit\.ly", description="no shortlinks", active=True
+            )
         )
         self.dbsession.commit()
         banword_update_svc = self._get_target_class()(self.dbsession)