Ejemplo n.º 1
0
def oauth_callback():
    error = request.args.get('error')
    url = url_for('.homepage')
    if not error:
        csrf = request.args.get('state')
        code = request.args.get('code')
        # look up CSRF token for remember value, returnto URI, and to confirm validity
        stored_csrf = CSRF.get(csrf=csrf, ip=get_ip())
        if stored_csrf is None:
            flash("CSRF token mismatch. Please try again.")
            return redirect(url, code=307)
        opts = json.loads(stored_csrf.opts)
        stored_csrf.delete()
        remember = opts.get('remember', False)
        url = opts.get('returnto', url)
        # hit oauth2/token for an authorization code, then hit oauth2/userinfo to get a name/tz
        user_data = check_mb_account(code)
        if user_data:
            (username, tz) = user_data
            Editor.add_or_update(username, tz)
            login_user(User(username, tz), remember=remember)
            flash("Logged in successfully!")
        else:
            flash("We couldn't log you in D:")
            url = url_for('.homepage')
    else:
        flash('There was an error: %s' % error)
    db.session.commit()
    return redirect(url, code=307)
Ejemplo n.º 2
0
    def test_delete(self):
        self.assertEqual(Editor.query.count(), 0)

        editor = Editor(name='Tester')
        db.session.add(editor)
        db.session.flush()
        self.assertEqual(Editor.query.count(), 1)

        editor.delete()
        self.assertEqual(Editor.query.count(), 0)
Ejemplo n.º 3
0
    def test_get(self):
        name = 'Tester'
        editor = Editor(name=name)
        db.session.add(editor)
        db.session.flush()

        same_editor = Editor.get(name)
        self.assertEqual(editor, same_editor)

        missing_editor = Editor.get('Unknown')
        self.assertIsNone(missing_editor)
Ejemplo n.º 4
0
    def test_get(self):
        name = "Tester"
        editor = Editor(name=name)
        db.session.add(editor)
        db.session.flush()

        same_editor = Editor.get(name)
        assert editor == same_editor

        missing_editor = Editor.get("Unknown")
        assert missing_editor is None
Ejemplo n.º 5
0
    def test_delete(self):
        editors = Editor.query.all()
        assert len(editors) == 0

        editor = Editor(name="Tester")
        db.session.add(editor)
        db.session.flush()

        editors = Editor.query.all()
        assert len(editors) == 1

        editor.delete()

        editors = Editor.query.all()
        assert len(editors) == 0
Ejemplo n.º 6
0
    def test_get(self):
        item = Item()
        db.session.add(item)
        db.session.flush()

        same_item = Item.get(item.id)
        self.assertEqual(item, same_item)

        missing_item = Editor.get("Unknown")
        self.assertIsNone(missing_item)
Ejemplo n.º 7
0
    def test_get(self):
        item = Item()
        db.session.add(item)
        db.session.flush()

        same_item = Item.get(item.id)
        assert item == same_item

        missing_item = Editor.get('Unknown')
        assert missing_item is None
Ejemplo n.º 8
0
    def test_add_or_update(self):
        self.assertEqual(Editor.query.count(), 0)

        editor_1 = Editor.add_or_update('Tester 1')

        editors = Editor.query.all()
        self.assertEqual(len(editors), 1)
        self.assertEqual(editors[0], editor_1)

        editor_2 = Editor.add_or_update('Tester 1', 'UTC')

        editors = Editor.query.all()
        self.assertEqual(len(editors), 1)
        self.assertEqual(editors[0], editor_2)
        self.assertEqual(editor_1, editor_2)

        editor_3 = Editor.add_or_update('Tester 2')

        editors = Editor.query.all()
        self.assertEqual(len(editors), 2)
        self.assertIn(editor_3, editors)
        self.assertNotEqual(editor_2, editor_3)
Ejemplo n.º 9
0
    def test_add_or_update(self):
        editors = Editor.query.all()
        assert len(editors) == 0

        editor_1 = Editor.add_or_update("Tester 1")

        editors = Editor.query.all()
        assert len(editors) == 1
        assert editors[0] == editor_1

        editor_2 = Editor.add_or_update("Tester 1", "UTC")

        editors = Editor.query.all()
        assert len(editors) == 1
        assert editors[0] == editor_2
        assert editor_1 == editor_2

        editor_3 = Editor.add_or_update("Tester 2")

        editors = Editor.query.all()
        assert len(editors) == 2
        assert editor_3 in editors
        assert editor_2 != editor_3
Ejemplo n.º 10
0
def load_user(username):
    editor = Editor.get(username)
    if editor is not None:
        return User(editor.name, editor.tz)