示例#1
0
    def test_path_for(self):
        project = pushpad.Pushpad(self._test_token, self._test_project_id)

        self.assertEqual(
            project.path_for('testuser1234'),
            "https://pushpad.xyz/projects/123/subscription/edit?uid=testuser1234&uid_signature=f1c94d68e25af9f8f818f7016b78934fec99d4c9"
        )
示例#2
0
# -*- coding: utf-8 -*-

import pushpad

user1 = 'user1'
user2 = 'user2'
user3 = 'user3'
users = [user1, user2, user3]

TOKEN = '5374d7dfeffa2eb49965624ba7596a09'
PROJ_ID = 123

project = pushpad.Pushpad(TOKEN, PROJ_ID)

print("HMAC signature for the uid: %s is: %s" %
      (user1, project.signature_for(user1)))
print("Subscribe anonymous to push notifications: %s" % (project.path()))
print("Subscribe current user: %s to push notifications: %s" %
      (user1, project.path_for(user1)))

notification = pushpad.Notification(project,
                                    body="Hello world!",
                                    title="Website Name",
                                    target_url="http://example.com")

print("Send notification to user: %s\nResult: %s" %
      (user1, notification.deliver_to(user1)))
print("Send notification to users: %s\nResult: %s" %
      (users, notification.deliver_to(users)))
print("Send broadcast notification\nResult: %s" % notification.broadcast())
示例#3
0
class TestNotification(unittest.TestCase):
    _test_token = '5374d7dfeffa2eb49965624ba7596a09'
    _test_project_id = 123

    _project = pushpad.Pushpad(_test_token, _test_project_id)

    def test_instantiate(self):
        """ notification can be instantiated"""

        notification = pushpad.Notification(self._project,
                                            body="Hello world!",
                                            title="Website Name",
                                            target_url="http://example.com")
        self.assertIsNotNone(notification)

    def test_set_body(self):
        """ can change body """
        notification = pushpad.Notification(self._project,
                                            body="Hello world!",
                                            title="Website Name",
                                            target_url="http://example.com")
        self.assertEqual(notification._body, "Hello world!")

    def test_set_title(self):
        """ can change title """
        notification = pushpad.Notification(self._project,
                                            body="Hello world!",
                                            title="Website Name",
                                            target_url="http://example.com")
        self.assertEqual(notification._title, "Website Name")

    def test_set_target_url(self):
        """ can change target_url """
        notification = pushpad.Notification(self._project,
                                            body="Hello world!",
                                            title="Website Name",
                                            target_url="http://example.com")
        self.assertEqual(notification._target_url, "http://example.com")

    def test_set_icon_url(self):
        """ can change icon_url """
        notification = pushpad.Notification(
            self._project,
            body="Hello world!",
            icon_url="http://example.com/assets/icon.png")
        self.assertEqual(notification._icon_url,
                         "http://example.com/assets/icon.png")

    def test_set_ttl(self):
        """ can change ttl """
        notification = pushpad.Notification(self._project,
                                            body="Hello world!",
                                            ttl=600)
        self.assertEqual(notification._ttl, 600)

    def test_req_headers(self):
        headers = {
            'Authorization': 'Token token="5374d7dfeffa2eb49965624ba7596a09"',
            'Content-Type': 'application/json;charset=UTF-8',
            'Accept': 'application/json',
        }
        notification = pushpad.Notification(self._project,
                                            body="Hello world!",
                                            title="Website Name",
                                            target_url="http://example.com")
        self.assertDictEqual(notification._req_headers(), headers)

    def test_req_body_all(self):
        body = {
            'notification': {
                'body': 'Hello world!',
                'title': 'Website Name',
                'target_url': 'http://example.com',
            }
        }
        notification = pushpad.Notification(self._project,
                                            body="Hello world!",
                                            title="Website Name",
                                            target_url="http://example.com")

        self.assertDictEqual(notification._req_body(), body)

    def test_req_body_list(self):
        body = {
            'notification': {
                'body': 'Hello world!',
                'title': 'Website Name',
                'target_url': 'http://example.com',
            },
            'uids': ('user1', 'user2', 'user3')
        }
        notification = pushpad.Notification(self._project,
                                            body="Hello world!",
                                            title="Website Name",
                                            target_url="http://example.com")

        self.assertDictEqual(
            notification._req_body(('user1', 'user2', 'user3')), body)

    def test_req_body_single(self):
        body = {
            'notification': {
                'body': 'Hello world!',
                'title': 'Website Name',
                'target_url': 'http://example.com',
            },
            'uids': 'user1'
        }
        notification = pushpad.Notification(self._project,
                                            body="Hello world!",
                                            title="Website Name",
                                            target_url="http://example.com")

        self.assertDictEqual(notification._req_body('user1'), body)

    def test_req_body_with_optional_fields(self):
        body = {
            'notification': {
                'body': 'Hello world!',
                'title': 'Website Name',
                'target_url': 'http://example.com',
                'icon_url': 'http://example.com/assets/icon.png',
                'ttl': 600
            }
        }
        notification = pushpad.Notification(
            self._project,
            body="Hello world!",
            title="Website Name",
            target_url="http://example.com",
            icon_url='http://example.com/assets/icon.png',
            ttl=600)

        self.assertDictEqual(notification._req_body(), body)

    @mock.patch('requests.post')
    def test_deliver(self, req_post_mock):
        body = {
            'notification': {
                'body': 'Hello world!',
                'title': 'Website Name',
                'target_url': 'http://example.com',
            },
            'uids': 'user1'
        }

        mock_response = mock.Mock()
        resp_json = {u'scheduled': 76}
        mock_response.status_code = 201
        mock_response.json.return_value = resp_json
        req_post_mock.return_value = mock_response

        notification = pushpad.Notification(self._project,
                                            body="Hello world!",
                                            title="Website Name",
                                            target_url="http://example.com")

        notification._deliver(body)
        req_post_mock.assert_called_once_with(
            'https://pushpad.xyz/projects/123/notifications',
            headers={
                'Content-Type': 'application/json;charset=UTF-8',
                'Accept': 'application/json',
                'Authorization':
                'Token token="5374d7dfeffa2eb49965624ba7596a09"'
            },
            json={
                'notification': {
                    'body': 'Hello world!',
                    'target_url': 'http://example.com',
                    'title': 'Website Name'
                },
                'uids': 'user1'
            })

    @mock.patch('pushpad.Notification._deliver')
    def test_broadcast(self, deliver_mock):
        notification = pushpad.Notification(self._project,
                                            body="Hello world!",
                                            title="Website Name",
                                            target_url="http://example.com")

        notification.broadcast()
        deliver_mock.assert_called_once_with({
            'notification': {
                'title': 'Website Name',
                'target_url': 'http://example.com',
                'body': 'Hello world!'
            }
        })

    @mock.patch('pushpad.Notification._deliver')
    def test_deliver_to(self, deliver_mock):
        notification = pushpad.Notification(self._project,
                                            body="Hello world!",
                                            title="Website Name",
                                            target_url="http://example.com")

        notification.deliver_to('user1')
        deliver_mock.assert_called_once_with(
            req_body={
                'notification': {
                    'body': 'Hello world!',
                    'target_url': 'http://example.com',
                    'title': 'Website Name'
                },
                'uids': 'user1'
            })

    @mock.patch('requests.post')
    def test_deliver_to_never_broadcasts(self, req_post_mock):
        notification = pushpad.Notification(self._project, body="Hello world!")

        mock_response = mock.Mock()
        mock_response.status_code = 201
        mock_response.json.return_value = {u'scheduled': 0}
        req_post_mock.return_value = mock_response

        notification.deliver_to(None)
        req_post_mock.assert_called_once_with(
            'https://pushpad.xyz/projects/123/notifications',
            headers={
                'Content-Type': 'application/json;charset=UTF-8',
                'Accept': 'application/json',
                'Authorization':
                'Token token="5374d7dfeffa2eb49965624ba7596a09"'
            },
            json={
                'notification': {
                    'body': 'Hello world!'
                },
                'uids': []
            })
示例#4
0
    def test_instantiate(self):
        """ pushpad can be instantiated"""

        project = pushpad.Pushpad(self._test_token, self._test_project_id)
        self.assertIsNotNone(project)
示例#5
0
    def test_get_path(self):
        project = pushpad.Pushpad(self._test_token, self._test_project_id)

        self.assertEqual(project.path(),
                         "https://pushpad.xyz/projects/123/subscription/edit")
示例#6
0
    def test_get_signature(self):
        data = "Lorem ipsum dolor sit amet, cu eam veniam verear blandit"
        data_sha1 = "71b88a1cab4fa14794128debecae12f5c091f7fe"

        project = pushpad.Pushpad(self._test_token, self._test_project_id)
        self.assertEqual(project.signature_for(data), data_sha1)
示例#7
0
    def test_set_project(self):
        """ can change project_id """

        project = pushpad.Pushpad(self._test_token, self._test_project_id)
        self.assertEqual(project.project_id, self._test_project_id)
示例#8
0
 def test_set_token(self):
     """ can change auth_token """
     project = pushpad.Pushpad(self._test_token, self._test_project_id)
     self.assertEqual(project.auth_token, self._test_token)
示例#9
0
from flaskext.mysql import MySQL
from wtforms import Form, StringField, TextAreaField, PasswordField, validators
from passlib.hash import sha256_crypt
from functools import wraps
from werkzeug.utils import secure_filename
from geopy.geocoders import Nominatim
from geopy.distance import great_circle

import pushpad
import smtplib
import nltk, string, os, sys
from sklearn.feature_extraction.text import TfidfVectorizer

app = Flask(__name__)

project = pushpad.Pushpad(auth_token='<auth Token>', project_id=5744)
# Config MySQL
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
app.config['MYSQL_DATABASE_USER'] = '******'
app.config['MYSQL_DATABASE_PASSWORD'] = '******'
app.config['MYSQL_DATABASE_DB'] = 'data'
app.config['MYSQL_CURSORCLASS'] = 'DictCursor'
# init MYSQL
mysql = MySQL()
mysql.init_app(app)


stemmer = nltk.stem.porter.PorterStemmer()
remove_punctuation_map = dict((ord(char), None) for char in string.punctuation)
gmail_user = '******'  
gmail_pwd = '<pwd>'
class TestNotification(unittest.TestCase):
    _test_token = '5374d7dfeffa2eb49965624ba7596a09'
    _test_project_id = 123

    _project = pushpad.Pushpad(_test_token, _test_project_id)

    def test_instantiate(self):
        self.assertIsNotNone(
            pushpad.Notification(self._project, body="Hello world!"))
        notification = pushpad.Notification(
            self._project,
            body="Hello world!",
            title="Website Name",
            target_url="http://example.com",
            icon_url="http://example.com/assets/icon.png",
            ttl=604800,
            require_interaction=True,
            image_url="http://example.com/assets/image.png",
            custom_data="123",
            custom_metrics=('examples', 'another_metric'),
            actions=({
                'title': "My Button 1",
                'target_url': "http://example.com/button-link",
                'icon': "http://example.com/assets/button-icon.png",
                'action': "myActionName"
            }, ),
            starred=True,
            send_at=datetime.datetime(2016, 7, 25, 10, 9, 0, 0))
        self.assertIsNotNone(notification)
        self.assertEqual(notification._body, "Hello world!")
        self.assertEqual(notification._title, "Website Name")
        self.assertEqual(notification._target_url, "http://example.com")
        self.assertEqual(notification._icon_url,
                         "http://example.com/assets/icon.png")
        self.assertEqual(notification._ttl, 604800)
        self.assertEqual(notification._require_interaction, True)
        self.assertEqual(notification._image_url,
                         "http://example.com/assets/image.png")
        self.assertEqual(notification._custom_data, "123")
        self.assertEqual(notification._custom_metrics,
                         ('examples', 'another_metric'))
        self.assertEqual(notification._actions, ({
            'title': "My Button 1",
            'target_url': "http://example.com/button-link",
            'icon': "http://example.com/assets/button-icon.png",
            'action': "myActionName"
        }, ))
        self.assertEqual(notification._starred, True)
        self.assertEqual(notification._send_at,
                         datetime.datetime(2016, 7, 25, 10, 9, 0, 0))

    def test_req_headers(self):
        headers = {
            'Authorization': 'Token token="5374d7dfeffa2eb49965624ba7596a09"',
            'Content-Type': 'application/json;charset=UTF-8',
            'Accept': 'application/json',
        }
        notification = pushpad.Notification(self._project, body="Hello world!")
        self.assertDictEqual(notification._req_headers(), headers)

    def test_req_body_with_optional_fields(self):
        body = {
            'notification': {
                'body':
                'Hello world!',
                'title':
                'Website Name',
                'target_url':
                'http://example.com',
                'icon_url':
                'http://example.com/assets/icon.png',
                'ttl':
                604800,
                'require_interaction':
                True,
                'image_url':
                'http://example.com/assets/image.png',
                'custom_data':
                '123',
                'custom_metrics': ('examples', 'another_metric'),
                'actions': ({
                    'title': 'My Button 1',
                    'target_url': 'http://example.com/button-link',
                    'icon': 'http://example.com/assets/button-icon.png',
                    'action': 'myActionName'
                }, ),
                'starred':
                True,
                'send_at':
                '2016-07-25T10:09'
            }
        }
        notification = pushpad.Notification(
            self._project,
            body="Hello world!",
            title="Website Name",
            target_url="http://example.com",
            icon_url="http://example.com/assets/icon.png",
            ttl=604800,
            require_interaction=True,
            image_url="http://example.com/assets/image.png",
            custom_data="123",
            custom_metrics=('examples', 'another_metric'),
            actions=({
                'title': "My Button 1",
                'target_url': "http://example.com/button-link",
                'icon': "http://example.com/assets/button-icon.png",
                'action': "myActionName"
            }, ),
            starred=True,
            send_at=datetime.datetime(2016, 7, 25, 10, 9, 0, 0))
        self.assertDictEqual(notification._req_body(), body)

    def test_req_body_uids(self):
        body = {
            'notification': {
                'body': 'Hello world!'
            },
            'uids': ('user1', 'user2', 'user3')
        }
        notification = pushpad.Notification(self._project, body="Hello world!")
        self.assertDictEqual(
            notification._req_body(('user1', 'user2', 'user3')), body)

    def test_req_body_uid(self):
        body = {'notification': {'body': 'Hello world!'}, 'uids': 'user1'}
        notification = pushpad.Notification(self._project, body="Hello world!")
        self.assertDictEqual(notification._req_body('user1'), body)

    def test_req_body_tags(self):
        body = {
            'notification': {
                'body': 'Hello world!'
            },
            'tags': ('tag1', 'tag2')
        }
        notification = pushpad.Notification(self._project, body="Hello world!")
        self.assertDictEqual(notification._req_body(tags=('tag1', 'tag2')),
                             body)

    def test_req_body_tag(self):
        body = {'notification': {'body': 'Hello world!'}, 'tags': 'tag1'}
        notification = pushpad.Notification(self._project, body="Hello world!")
        self.assertDictEqual(notification._req_body(tags='tag1'), body)

    @mock.patch('requests.post')
    def test_deliver(self, req_post_mock):
        body = {
            'notification': {
                'body': 'Hello world!',
                'title': 'Website Name',
                'target_url': 'http://example.com',
            },
            'uids': 'user1'
        }

        mock_response = mock.Mock()
        resp_json = {u'scheduled': 76}
        mock_response.status_code = 201
        mock_response.json.return_value = resp_json
        req_post_mock.return_value = mock_response

        notification = pushpad.Notification(self._project,
                                            body="Hello world!",
                                            title="Website Name",
                                            target_url="http://example.com")

        notification._deliver(body)
        req_post_mock.assert_called_once_with(
            'https://pushpad.xyz/projects/123/notifications',
            headers={
                'Content-Type': 'application/json;charset=UTF-8',
                'Accept': 'application/json',
                'Authorization':
                'Token token="5374d7dfeffa2eb49965624ba7596a09"'
            },
            json={
                'notification': {
                    'body': 'Hello world!',
                    'target_url': 'http://example.com',
                    'title': 'Website Name'
                },
                'uids': 'user1'
            })

    @mock.patch('pushpad.Notification._deliver')
    def test_broadcast(self, deliver_mock):
        notification = pushpad.Notification(self._project,
                                            body="Hello world!",
                                            title="Website Name",
                                            target_url="http://example.com")

        notification.broadcast()
        deliver_mock.assert_called_once_with({
            'notification': {
                'title': 'Website Name',
                'target_url': 'http://example.com',
                'body': 'Hello world!'
            }
        })

    @mock.patch('pushpad.Notification._deliver')
    def test_broadcast_with_tags(self, deliver_mock):
        notification = pushpad.Notification(self._project, body="Hello world!")
        notification.broadcast(tags=('tag1', 'tag2'))
        deliver_mock.assert_called_once_with({
            'notification': {
                'body': 'Hello world!'
            },
            'tags': ('tag1', 'tag2')
        })

    @mock.patch('pushpad.Notification._deliver')
    def test_deliver_to(self, deliver_mock):
        notification = pushpad.Notification(self._project,
                                            body="Hello world!",
                                            title="Website Name",
                                            target_url="http://example.com")

        notification.deliver_to('user1')
        deliver_mock.assert_called_once_with(
            req_body={
                'notification': {
                    'body': 'Hello world!',
                    'target_url': 'http://example.com',
                    'title': 'Website Name'
                },
                'uids': 'user1'
            })

    @mock.patch('pushpad.Notification._deliver')
    def test_deliver_to_with_tags(self, deliver_mock):
        notification = pushpad.Notification(self._project, body="Hello world!")
        notification.deliver_to(('user1', 'user2'), tags=('tag1', 'tag2'))
        deliver_mock.assert_called_once_with(
            req_body={
                'notification': {
                    'body': 'Hello world!'
                },
                'uids': ('user1', 'user2'),
                'tags': ('tag1', 'tag2')
            })

    @mock.patch('requests.post')
    def test_deliver_to_never_broadcasts(self, req_post_mock):
        notification = pushpad.Notification(self._project, body="Hello world!")

        mock_response = mock.Mock()
        mock_response.status_code = 201
        mock_response.json.return_value = {u'scheduled': 0}
        req_post_mock.return_value = mock_response

        notification.deliver_to(None)
        req_post_mock.assert_called_once_with(
            'https://pushpad.xyz/projects/123/notifications',
            headers={
                'Content-Type': 'application/json;charset=UTF-8',
                'Accept': 'application/json',
                'Authorization':
                'Token token="5374d7dfeffa2eb49965624ba7596a09"'
            },
            json={
                'notification': {
                    'body': 'Hello world!'
                },
                'uids': []
            })