Example #1
0
 def test_push_msg(self, mock):
     """Test push_msg works."""
     client = PybossaOneSignal(app_id="1", api_key="key")
     fakeRequest = MagicMock()
     fakeRequest.status_code = 200
     fakeRequest.reason = 'OK'
     fakeRequest.json.return_value = self.valid_notification
     mock.return_value = fakeRequest
     tmp = client.push_msg()
     assert tmp[0] == 200
     assert tmp[1] == 'OK'
     assert tmp[2] == self.valid_notification
Example #2
0
 def test_create_app(self, mock):
     """Test create_app works."""
     client = PybossaOneSignal(app_id="1", api_key="algo", auth_key="key")
     fakeRequest = MagicMock()
     fakeRequest.status_code = 200
     fakeRequest.reason = 'OK'
     fakeRequest.json.return_value = self.valid_app
     mock.return_value = fakeRequest
     tmp = client.create_app('name', 'https://scifabric.com',
                             'https://scifabric.com/img')
     assert tmp[0] == 200
     assert tmp[1] == 'OK'
     assert tmp[2] == self.valid_app
Example #3
0
def push_notification(project_id, **kwargs):
    """Send push notification."""
    from pybossa.core import project_repo
    project = project_repo.get(project_id)
    if project.info.get('onesignal'):
        app_id = current_app.config.get('ONESIGNAL_APP_ID') 
        api_key = current_app.config.get('ONESIGNAL_API_KEY')
        client = PybossaOneSignal(app_id=app_id, api_key=api_key)
        filters = [{"field": "tag", "key": project_id, "relation": "exists"}]
        return client.push_msg(contents=kwargs['contents'],
                               headings=kwargs['headings'],
                               launch_url=kwargs['launch_url'],
                               web_buttons=kwargs['web_buttons'],
                               filters=filters)
Example #4
0
def push_notification(project_id, **kwargs):
    """Send push notification."""
    from pybossa.core import project_repo
    project = project_repo.get(project_id)
    if project.info.get('onesignal'):
        app_id = current_app.config.get('ONESIGNAL_APP_ID')
        api_key = current_app.config.get('ONESIGNAL_API_KEY')
        client = PybossaOneSignal(app_id=app_id, api_key=api_key)
        filters = [{"field": "tag", "key": project_id, "relation": "exists"}]
        return client.push_msg(contents=kwargs['contents'],
                               headings=kwargs['headings'],
                               launch_url=kwargs['launch_url'],
                               web_buttons=kwargs['web_buttons'],
                               filters=filters)
Example #5
0
    def test_push_msg_app_ids(self, mock):
        """Test push_msg with array app_ids works."""
        client = PybossaOneSignal(app_ids=["1", "2"], api_key="key")
        fakeRequest = MagicMock()
        fakeRequest.status_code = 200
        fakeRequest.reason = 'OK'
        fakeRequest.json.return_value = self.valid_notification
        mock.return_value = fakeRequest
        tmp = client.push_msg()
        assert tmp[0] == 200
        assert tmp[1] == 'OK'
        assert tmp[2] == self.valid_notification

        self.payload['app_ids'] = ["1", "2"]

        mock.assert_called_with(client.api_url, 
                                headers=client.header("key"),
                                json=self.payload)
Example #6
0
    def test_push_msg_include_priority(self, mock):
        """Test push_msg with array priority works."""
        client = PybossaOneSignal(app_id="1", api_key="key")
        fakeRequest = MagicMock()
        fakeRequest.status_code = 200
        fakeRequest.reason = 'OK'
        fakeRequest.json.return_value = self.valid_notification
        mock.return_value = fakeRequest
        tmp = client.push_msg(priority="10")
        assert tmp[0] == 200
        assert tmp[1] == 'OK'
        assert tmp[2] == self.valid_notification

        self.payload['app_id'] = "1"
        self.payload['priority'] = "10"

        mock.assert_called_with(client.api_url, 
                                headers=client.header("key"),
                                json=self.payload)
Example #7
0
    def test_push_msg_include_send_after(self, mock):
        """Test push_msg with array send_after works."""
        client = PybossaOneSignal(app_id="1", api_key="key")
        fakeRequest = MagicMock()
        fakeRequest.status_code = 200
        fakeRequest.reason = 'OK'
        fakeRequest.json.return_value = self.valid_notification
        mock.return_value = fakeRequest
        tmp = client.push_msg(send_after="Thu Sep 24 2015 14:00:00 GMT-0700 (PDT)")
        assert tmp[0] == 200
        assert tmp[1] == 'OK'
        assert tmp[2] == self.valid_notification

        self.payload['app_id'] = "1"
        self.payload['send_after'] = "Thu Sep 24 2015 14:00:00 GMT-0700 (PDT)"

        mock.assert_called_with(client.api_url, 
                                headers=client.header("key"),
                                json=self.payload)
Example #8
0
def create_onesignal_app(project_id):
    """Create onesignal app."""
    from flask import url_for
    from pybossa.core import project_repo
    auth_key = current_app.config.get('ONESIGNAL_AUTH_KEY')
    if auth_key:
        project = project_repo.get(project_id)
        chrome_web_origin = url_for('project.details',
                                    short_name=project.short_name,
                                    _external=True)
        chrome_web_default_notification_icon = project.info.get('thumbnail_url')
        client = PybossaOneSignal(auth_key=auth_key)
        res = client.create_app(project.short_name,
                                chrome_web_origin,
                                chrome_web_default_notification_icon)
        
        if res[0] == 200:
            project.info['onesignal'] = res[2]
            project.info['onesignal_app_id'] = res[2]['id']
            project_repo.update(project)
        return res
Example #9
0
 def test_push_msg_no_api_key(self):
     """Test push_msg without api_key."""
     client = PybossaOneSignal(app_id="1", auth_key="something")
     client.push_msg()
Example #10
0
 def test_proper_init(self):
     """Test init adds header."""
     client = PybossaOneSignal(app_id="1", api_key="key")
     assert client.api_key == "key"
     assert client.auth_key == None
Example #11
0
 def test_headers(self):
     """Test headers method."""
     client = PybossaOneSignal(app_id="1", api_key="key")
     headers = client.header("key")
     assert headers['Content-Type'] == 'application/json; charset=utf-8'
     assert headers['Authorization'] == 'Basic key'
Example #12
0
 def test_create_client(self):
     """Test create client without keys."""
     PybossaOneSignal()
Example #13
0
 def test_init_no_app_id_duplicates(self):
     """Test init using app_id and app_ids."""
     PybossaOneSignal(app_id="1", app_ids=["a"], api_key="something")
Example #14
0
 def test_create_app_no_auth_key(self):
     """Test create_app without auth_key."""
     client = PybossaOneSignal(api_key="key")
     client.create_app('name', 'https://scifabric.com',
                       'https://scifabric.com/img')