Exemplo n.º 1
0
 def test_lookup_user(self):
     with db.connect() as connection:
         self.assertEqual(
             db.lookup_user(connection, DEVELOPERS["iugov"]).id,
             DEVELOPERS["iugov"])
         self.assertFalse(
             db.lookup_user(connection, get_random_id(connection)))
         self.assertFalse(
             db.lookup_user(connection, get_random_id(connection)))
Exemplo n.º 2
0
def start(update: Update, context: CallbackContext):
    """Called on `/start`. Add/update user and introduce them to the bot."""
    user = update.message.from_user

    with db.connect() as connection:
        user = db.lookup_user(connection, update.message.from_user.id)
        if not user:
            db.add_user(
                connection,
                update.message.from_user,
                timestamp=update.effective_message.date,
            )
        elif user.first_name != update.message.from_user.first_name:
            db.update_user(connection, update.message.from_user, user.id)

    context.bot.send_photo(
        chat_id=update.message.chat_id,
        photo=
        "https://github.com/iugov/s4lbot/blob/develop/images/cover.jpg?raw=true",
    )

    context.bot.send_message(
        chat_id=update.message.chat_id,
        text="{}\n\n{}".format(PROMPTS["welcome"], PROMPTS["alpha"]),
        reply_markup=keyboards.home(),
        parse_mode=telegram.ParseMode.MARKDOWN,
    )
Exemplo n.º 3
0
    def test_delete_user(self):
        with db.connect() as connection:
            user = User(id=get_random_id(connection),
                        first_name="Augustus",
                        is_bot=False)

            db.add_user(connection, user, datetime.utcnow())

            db.delete_user(connection, user.id)

            result = db.lookup_user(connection, user.id)
            self.assertFalse(result)
Exemplo n.º 4
0
    def test_update_user(self):
        with db.connect() as connection:
            uid = get_random_id(connection)
            user = User(id=uid, first_name="Augustus", is_bot=False)

            db.add_user(connection, user, datetime.utcnow())

            updated_user = User(id=uid, first_name="Filamentis", is_bot=False)

            db.update_user(connection, updated_user, user.id)

            result = db.lookup_user(connection, user.id)
            self.assertEqual(result.id, updated_user.id)
            self.assertEqual(result.first_name, updated_user.first_name)

            db.delete_user(connection, user.id)
Exemplo n.º 5
0
    def test_add_user(self):
        with db.connect() as connection:
            timestamp = datetime.now()
            user = User(
                id=get_random_id(connection),
                first_name="Augustus",
                is_bot=False,
                joined_on=timestamp,
            )

            db.add_user(connection, user, timestamp)

            result = db.lookup_user(connection, user.id)
            self.assertTrue(result)
            self.assertEqual(result.id, user.id)
            self.assertEqual(result.first_name, user.first_name)
            self.assertEqual(result.joined_on.ctime(), timestamp.ctime())

            db.delete_user(connection, user.id)
Exemplo n.º 6
0
def authenticate():
    flow = flow_from_clientsecrets(
        CLIENT_SECRETS,
        scope='https://www.googleapis.com/auth/userinfo.email',
        redirect_uri=url_for('authenticate', _external=True))

    if 'code' not in request.args:  #first step of authentication
        auth_uri = flow.step1_get_authorize_url()  #google login page
        return redirect(auth_uri)  # Redirects to that page
    else:  #login will redirect here wiht 'code' in args
        auth_code = request.args.get('code')
        credentials = flow.step2_exchange(auth_code)  #second step
        session['credentials'] = credentials.to_json()  #convert cred to json
        c = json.loads(session['credentials'])
        (user, domain) = tuple(c['id_token']['email'].split('@'))
        user_details = db.lookup_user(user)
        if domain == 'stuy.edu' and user_details:
            session['user'] = user
            session['name'] = user_details['name']
            session['rights'] = user_details['rights']
            return redirect(url_for('root'))
        else:
            flash(user + ' is not an approved user for this serive.')
            return redirect(url_for('root'))