Ejemplo n.º 1
0
 def test_sms_user_creation(self):
     registered_user = User(username='******',
                            password='******',
                            number='+16666666666')
     registered_user.save()
     
     number = '+15555555555'
     name = 'Testy Smoth'
     
     user = User(number=number, display_name=name)
     
     self.assertEqual(user[u'number'], number)
     self.assertEqual(user[u'display_name'], name)
     self.assertEqual(user[u'username'], None)
     
     user.save()
     
     self.assertEqual(user[u'number'], number)
     self.assertEqual(user[u'display_name'], name)
     self.assertEqual(user[u'username'], None)
     
     got_user = User.get({u'number':number})
     
     self.assertEqual(got_user[u'number'], number)
     self.assertEqual(got_user[u'display_name'], name)
     self.assertEqual(got_user[u'username'], None)
Ejemplo n.º 2
0
 def test_comment_field_created(self):
     user = User(username='******',
                 password='******',
                 number='+16666666666')
     user.save()
     
     event = Event(**{
         u'where': 'test',
         u'when': timestamp(),
         u'what': 'test',
         u'broadcast': False,
         u'posted_from': [37.422834216666665, -122.08536667833332],
         u'creator': user[u'username'],
     })
     event.save()
     
     comment = 'the test comment'
     
     post = self.post('/events/%s/comments/' % event[u'id'], 
                          {'comment': comment})
     
     self.assertEqual(post.status, 200, 'comment POST 200')
     
     comment = Comment(user=self.get_user()[u'id'], event=event[u'id'])
     
     self.assertTrue(u'created' in comment, 'comment has created')
     
     get = self.get('/events/%s/comments/' % event[u'id'])
     self.assertEqual(get.status, 200, 'got comment')
     
     got_comment = json.loads(get.read())['comments'][0]
     self.assertTrue(u'created' in got_comment, 'comment has created')
Ejemplo n.º 3
0
 def prep_for_response_sms(self):
     user = User(username='******',
                 password='******',
                 number='+16666666666')
     user.save()
     
     event = Event(**{
         u'where': 'test',
         u'when': timestamp(),
         u'what': 'test',
         u'broadcast': False,
         u'posted_from': [37.422834216666665, -122.08536667833332],
         u'creator': user[u'username'],
     })
     event.save()
     
     smsuser = User(number='+16475555555', display_name='Testy Smoth')
     smsuser.save()
     
     Attendant(user=user[u'id'], event=event[u'id']).save()
     
     SMSRegister(contact_number=smsuser[u'number'], 
                 twilio_number=settings.TWILIO_NUMBERS[0], 
                 event=event[u'id'], 
                 expires=event[u'when'],
                 user=user[u'id']).save()
     
     return user, event, smsuser
Ejemplo n.º 4
0
    def setUp(self):
        with open('api/test_data/test_data.json') as f:
            self.test_data = json.load(f)

        self.factory = RequestFactory()
        self.token = get_anonymous_user_token()
        self.auth_token = get_authenticated_user_token()
        self.test_uuid = str(uuid.uuid4())
        self.user = FirebaseUser(self.test_data["users"]["firebase_data"])
        u = User(self.test_uuid, 'display name', photo_url='')
        u.save(db)
Ejemplo n.º 5
0
 def make_user(self, username=None):
     if username is None:
         self.__user_number__ += 1
         username = '******' % self.__user_number__
     u = User(**{
             u'username': username, 
             u'password': User.hash_password(self._gen_user_password_),
             u'number': '+16656656665',
             u'revision': uuid.uuid1().hex,
             u'created': timestamp(),
         })
     u.save()
     return u
Ejemplo n.º 6
0
 def test_sms_comment(self):
     display_name = 'Test User display'
     user = User(number='+16666666666', 
                 display_name=display_name)
     user.save()
     
     event = Event(**{
         u'where': 'test',
         u'when': timestamp(),
         u'what': 'test',
         u'broadcast': False,
         u'posted_from': [37.422834216666665, -122.08536667833332],
         u'creator': user[u'username'],
     })
     event.save()
     
     comment = 'the test comment'
     
     Comment(**{
         u'comment': comment,
         u'event': event[u'id'],
         u'user': user[u'id']
     }).save()
     
     get = self.get('/events/%s/comments/' % event[u'id'])
     
     self.assertEqual(get.status, 200, 'comment GET 200')
     
     try:
         get = json.loads(get.read())
     except ValueError:
         self.assertTrue(False, 'comment GET not JSON')
     
     self.assertTrue('comments' in get, 
                     'comments returned')
     self.assertEqual(len(get['comments']), 1, 
                      'correct number of comments')
     
     self.assertFalse('username' in get['comments'][0], 
                     'username field unavailable')
     self.assertTrue('display_name' in get['comments'][0], 
                     'display_name field available')
     self.assertEqual(get['comments'][0]['display_name'], display_name, 
                      'display_name field set correctly')
Ejemplo n.º 7
0
 def test_new_comment(self):
     user = User(username='******',
                 password='******',
                 number='+16666666666')
     user.save()
     
     event = Event(**{
         u'where': 'test',
         u'when': timestamp(),
         u'what': 'test',
         u'broadcast': False,
         u'posted_from': [37.422834216666665, -122.08536667833332],
         u'creator': user[u'username'],
     })
     event.save()
     
     comment = 'the test comment'
     
     post = self.post('/events/%s/comments/' % event[u'id'], 
                          {'comment': comment})
     
     self.assertEqual(post.status, 200, 'comment POST 200')
     
     get = self.get('/events/%s/comments/' % event[u'id'])
     
     self.assertEqual(get.status, 200, 'comment GET 200')
     
     try:
         get = json.loads(get.read())
     except ValueError:
         self.assertTrue(False, 'comment GET not JSON')
     
     self.assertTrue('comments' in get, 'comment GET has comments')
     self.assertEqual(len(get['comments']), 1, 'comment GET return 1 '
                      'comment')
     self.assertTrue('comment' in get['comments'][0], 'comment GET has '
                     'comments')
     self.assertEqual(get['comments'][0]['comment'], comment, 'comment GET '
                      'returned the right comment')
     self.assertTrue('username' in get['comments'][0], 
                     'comment GET has username')
     self.assertFalse('display_name' in get['comments'][0], 
                     'comment GET does not have display_name')
Ejemplo n.º 8
0
 def test_sms_notification_creation(self):
     if not settings.TEST_SMS: return
     
     user = User(username='******',
                 password='******',
                 number='+16666666666')
     user.save()
     
     event = Event(**{
         u'where': 'test',
         u'when': timestamp(),
         u'what': 'test',
         u'broadcast': False,
         u'posted_from': [37.422834216666665, -122.08536667833332],
         u'creator': user[u'username'],
     })
     event.save()
     
     number = '+16475551234'
     name = 'Testy Smoth'
     
     reg, out, is_user = SMS.register(event, [{u'number':number, u'name':name},
                                              {u'number':user[u'number'], 
                                               u'name':name}])
     
     self.assertEqual(len(reg), 1, 'correct ammout registered')
     self.assertEqual(len(out), 0, 'correct ammout out of numbers')
     self.assertEqual(len(is_user), 1, 'correct ammout are already users')
     
     reged = reg[0]
     
     self.assertEqual(reged.__class__, User)
     self.assertTrue(u'id' in reged)
     
     result = notifications.send(reged[u'id'], 
                                 {u'type': 'invite', 
                                  u'event_revision': event[u'revision'],
                                  u'event_id': event[u'id']})
     
     self.assertEqual(len(result), 1, 'the correct number of notifications '
                                      'were sent')
     self.assertTrue(result[0], 'the notification was sent correctly')
Ejemplo n.º 9
0
    def save_instance(self, instance, using_transactions, real_dry_run):
        if not real_dry_run:
            try:
                # extra logic if object already exist
                user = User.objects.get(email=instance.email)
                pass
            except Exception:
                # create new object
                user = User(id=instance.id)

                password = instance.password
                password = make_password(password)

                created_by = self.context['request'].user
                project = self.context['request'].user.project

                user.created_by = created_by
                user.project = project
                user.password = password
                user.save()
Ejemplo n.º 10
0
def register(event, contacts, tz=None):
    # late import to User model can use phone number normalizer
    from api.users.models import User

    if not u"id" in event:
        event[u"id"] = str(event[u"_id"])

    registered = list()
    out_of_numbers = list()
    has_username = list()

    for contact in contacts:
        contact[u"number"] = contact[u"number"]
        user = User.get({u"number": contact[u"number"]})
        if user is None:
            user = User(number=contact[u"number"], display_name=contact[u"name"])
            user.save()

        # Connectsy users don't get SMS
        if user[u"username"] is not None:
            has_username.append(user)
            continue

        # make sure this user still has numbers available
        potential_numbers = [n for n in settings.TWILIO_NUMBERS]
        for reg in SMSRegister.find({u"contact_number": contact[u"number"]}):
            if from_timestamp(reg[u"expires"]) > datetime.now(pytz.utc):
                potential_numbers = [n for n in potential_numbers if n != reg[u"twilio_number"]]
        if len(potential_numbers) == 0:
            out_of_numbers.append(contact)
            continue

        if tz is None:
            tz = "America/Toronto"

        if event[u"when"]:
            expires = event[u"when"]
        else:
            print "created", event[u"created"]
            expires = timestamp(dt=from_timestamp(event[u"created"]) + timedelta(days=2))

        registered.append(user)
        r = SMSRegister(
            contact_number=contact[u"number"],
            twilio_number=potential_numbers[0],
            event=event[u"id"],
            expires=expires,
            user=user[u"id"],
            tz=tz,
        )
        r.save()

        # register with the notifications system as well
        note = {
            u"user": user[u"id"],
            u"timestamp": timestamp(),
            u"client_type": "SMS",
            u"client_id": r[u"contact_number"],
        }
        NotificationRegister(**note).save()

    return registered, out_of_numbers, has_username