Пример #1
0
 def test_unsubscribe(self):
     db.create_all()
     u = User(email='*****@*****.**', username='******', password='******')
     t = Talk(title='t', description='d', author=u)
     c1 = Comment(talk=t,
                  body='c1',
                  author_name='n',
                  author_email='*****@*****.**',
                  approved=True,
                  notify=True)
     c2 = Comment(talk=t,
                  body='c2',
                  author_name='n',
                  author_email='*****@*****.**',
                  approved=False,
                  notify=True)
     c3 = Comment(talk=t,
                  body='c3',
                  author_name='n',
                  author_email='*****@*****.**',
                  approved=False,
                  notify=True)
     db.session.add_all([u, t, c1, c2, c3])
     db.session.commit()
     token = t.get_unsubscribe_token(u'*****@*****.**')
     Talk.unsubscribe_user(token)
     comments = t.comments.all()
     for comment in comments:
         if comment.author_email == '*****@*****.**':
             self.assertTrue(comment.notify == False)
         else:
             self.assertTrue(comment.notify == True)
Пример #2
0
def edit_talk(id=None):
    talk = Talk() if id is None else Talk.query.get(id)

    if id is not None and talk is None:
        return abort(404)
    if not current_user.is_admin and (
            id is not None and not talk.can_edit(current_user)
            or len(current_user.edited_collections) == 0):
        return abort(403)
    if request.args.get("copy", False):
        talk = copy_row(talk, ["id"])

    next = request.args.get("next")
    if not is_safe_url(next):
        return abort(400)
    else:
        next = next or url_for("core.talks")

    is_new = talk.id is None
    form = TalkForm(obj=talk)

    if form.validate_on_submit():
        form.populate_obj(talk)
        HistoryItem.build_for(talk)
        if is_new:
            db.session.add(talk)
        db.session.commit()
        return redirect(next)

    return render_template("core/talk_edit.html",
                           title="Talk",
                           form=form,
                           new=is_new,
                           next=next,
                           talk=talk)
Пример #3
0
 def test_bad_unsubscribe_token(self):
     talk, email = Talk.unsubscribe_user('an invalid token')
     self.assertIsNone(talk)
     self.assertIsNone(email)
     u = User(email='*****@*****.**', username='******', password='******')
     t = Talk(title='t', description='d', author=u)
     token = t.get_unsubscribe_token('*****@*****.**')
     talk, email = Talk.unsubscribe_user(token)
     self.assertIsNone(talk)
     self.assertIsNone(email)
Пример #4
0
 def test_bad_unsubscribe_token(self):
     talk, email = Talk.unsubscribe_user('an invalid token')
     self.assertIsNone(talk)
     self.assertIsNone(email)
     u = User(email='*****@*****.**', username='******', password='******')
     t = Talk(title='t', description='d', author=u)
     token = t.get_unsubscribe_token('*****@*****.**')
     talk, email = Talk.unsubscribe_user(token)
     self.assertIsNone(talk)
     self.assertIsNone(email)
Пример #5
0
def get_talk():
    user = User.query.get(session['user_id'])
    talk_text = request.form.get('talk')
    text_id = request.form.get('text_id')
    if not all([talk_text]):
        return jsonify(result = 1)
    talk = Talk()
    talk.article_id = text_id
    talk.content = talk_text
    talk.user = user.id
    db.session.add(talk)
    db.session.commit()
    return jsonify(result = 0)
Пример #6
0
 def test_approved(self):
     db.create_all()
     u = User(email='*****@*****.**', username='******', password='******')
     t = Talk(title='t', description='d', author=u)
     c1 = Comment(talk=t, body='c1', author_name='n',
                  author_email='*****@*****.**', approved=True)
     c2 = Comment(talk=t, body='c2', author_name='n',
                  author_email='*****@*****.**', approved=False)
     db.session.add_all([u, t, c1, c2])
     db.session.commit()
     approved = t.approved_comments().all()
     self.assertTrue(len(approved) == 1)
     self.assertTrue(approved[0] == c1)
Пример #7
0
 def test_moderation(self):
     db.create_all()
     u1 = User(email='*****@*****.**', username='******', password='******')
     u2 = User(email='*****@*****.**',
               username='******',
               password='******',
               is_admin=True)
     t = Talk(title='t', description='d', author=u1)
     c1 = Comment(talk=t,
                  body='c1',
                  author_name='n',
                  author_email='*****@*****.**',
                  approved=True)
     c2 = Comment(talk=t,
                  body='c2',
                  author_name='n',
                  author_email='*****@*****.**',
                  approved=False)
     db.session.add_all([u1, u2, t, c1, c2])
     db.session.commit()
     for_mod1 = u1.for_moderation().all()
     for_mod1_admin = u1.for_moderation(True).all()
     for_mod2 = u2.for_moderation().all()
     for_mod2_admin = u2.for_moderation(True).all()
     self.assertEqual(len(for_mod1), 1)
     self.assertEqual(for_mod1[0], c2)
     self.assertEqual(for_mod1_admin, for_mod1)
     self.assertEqual(len(for_mod2), 0)
     self.assertEqual(len(for_mod2_admin), 1)
     self.assertEqual(for_mod2_admin[0], c2)
Пример #8
0
def talks():
    form = TalkForm(request.form)

    if request.method == 'POST' and form.validate():
        content = form.content.data
        try:
            t = Talk(content=content)
            db.session.add(t)
            db.session.commit()
        except Exception as e:
            db.session.rollback()
            flash(e.message, category='error')
        else:
            flash('话唠,闲的慌是不?')
        return redirect(url_for('main.talks'))

    page = request.args.get('page', 1, type=int)
    pagination = Talk.query.order_by(Talk.createTime.desc()).paginate(
        page, per_page=current_app.config['TALKS_PER_PAGE'])
    talks = [TalkViewModel(t) for t in pagination.items]
    talks = TalksViewModel(talks)
    return render_template('main/talks.html',
                           form=form,
                           talks=talks,
                           pagination=pagination)
Пример #9
0
    def test_delete(self):
        u1 = User(email='*****@*****.**', username='******', password='******')
        u2 = User(email='*****@*****.**', username='******', password='******')
        t = Talk(title='t', description='d', author=u1)
        c = Comment(talk=t, body='c1', author_name='n',
                    author_email='*****@*****.**', approved=False)
        db.session.add_all([u1, u2, t, c])
        db.session.commit()

        # wrong user --> 403
        token = u2.get_api_token()
        with self.app.test_request_context(
                '/api/1.0/comments/' + str(c.id),
                method='DELETE',
                data=json.dumps({'token': token}),
                headers={'Content-Type': 'application/json'}):
            res = self.app.full_dispatch_request()
            self.assertTrue(res.status_code == 403)

        token = u1.get_api_token()
        with self.app.test_request_context(
                '/api/1.0/comments/' + str(c.id),
                method='DELETE',
                data=json.dumps({'token': token}),
                headers={'Content-Type': 'application/json'}):
            res = self.app.full_dispatch_request()
            self.assertTrue(res.status_code == 200)
            c = Comment.query.get(c.id)
            self.assertIsNone(c)
 def test_queue(self):
     db.create_all()
     u = User(email='*****@*****.**', username='******', password='******')
     t1 = Talk(title='t1', description='d', author=u)
     t2 = Talk(title='t2', description='d', author=u)
     p = PendingEmail(name='n',
                      email='*****@*****.**',
                      subject='s',
                      body_text='t',
                      body_html='h',
                      talk=t1)
     db.session.add_all([u, t1, t2, p])
     db.session.commit()
     self.assertTrue(PendingEmail.already_in_queue('*****@*****.**', t1) is True)
     self.assertTrue(
         PendingEmail.already_in_queue('*****@*****.**', t1) is False)
     self.assertTrue(PendingEmail.already_in_queue('*****@*****.**', t2) is False)
Пример #11
0
 def test_approved(self):
     db.create_all()
     u = User(email='*****@*****.**', username='******', password='******')
     t = Talk(title='t', description='d', author=u)
     c1 = Comment(talk=t,
                  body='c1',
                  author_name='n',
                  author_email='*****@*****.**',
                  approved=True)
     c2 = Comment(talk=t,
                  body='c2',
                  author_name='n',
                  author_email='*****@*****.**',
                  approved=False)
     db.session.add_all([u, t, c1, c2])
     db.session.commit()
     approved = t.approved_comments().all()
     self.assertTrue(len(approved) == 1)
     self.assertTrue(approved[0] == c1)
Пример #12
0
 def test_unsubscribe(self):
     db.create_all()
     u = User(email='*****@*****.**', username='******', password='******')
     t = Talk(title='t', description='d', author=u)
     c1 = Comment(talk=t, body='c1', author_name='n',
                  author_email='*****@*****.**', approved=True, notify=True)
     c2 = Comment(talk=t, body='c2', author_name='n',
                  author_email='*****@*****.**', approved=False, notify=True)
     c3 = Comment(talk=t, body='c3', author_name='n',
                  author_email='*****@*****.**', approved=False, notify=True)
     db.session.add_all([u, t, c1, c2, c3])
     db.session.commit()
     token = t.get_unsubscribe_token(u'*****@*****.**')
     Talk.unsubscribe_user(token)
     comments = t.comments.all()
     for comment in comments:
         if comment.author_email == '*****@*****.**':
             self.assertTrue(comment.notify == False)
         else:
             self.assertTrue(comment.notify == True)
Пример #13
0
 def talks(self, count=50):
     i = 0
     with db.autoCommit():
         while i < count:
             t = Talk(
                 content=self.fake.paragraph(nb_sentences=1),
                 createTime=self.fake.date_time(),
                 updateTime=self.fake.date_time())
             db.session.add(t)
             i += 1
     return count
    def test_approve(self):
        u1 = User(email='*****@*****.**', username='******', password='******')
        u2 = User(email='*****@*****.**', username='******', password='******')
        t = Talk(title='t', description='d', author=u1)
        c = Comment(talk=t,
                    body='c1',
                    author_name='n',
                    author_email='*****@*****.**',
                    approved=False)
        db.session.add_all([u1, u2, t, c])
        db.session.commit()

        # wrong user --> 403
        token = u2.get_api_token()
        with self.app.test_request_context(API.format(c.id),
                                           method='PUT',
                                           data=json.dumps({'token': token}),
                                           headers=JSON_HEADER):
            res = self.app.full_dispatch_request()
            self.assertTrue(res.status_code == 403)

        # correct user --> 200
        token = u1.get_api_token()
        with self.app.test_request_context(API.format(c.id),
                                           method='PUT',
                                           data=json.dumps({'token': token}),
                                           headers=JSON_HEADER):
            res = self.app.full_dispatch_request()
            self.assertTrue(res.status_code == 200)
            c = Comment.query.get(c.id)
            self.assertTrue(c.approved)

        # approve an already approved comment --> 400
        with self.app.test_request_context(API.format(c.id),
                                           method='PUT',
                                           data=json.dumps({'token': token}),
                                           headers=JSON_HEADER):
            res = self.app.full_dispatch_request()
            self.assertTrue(res.status_code == 400)

        # delete an already approved comment --> 400
        with self.app.test_request_context(API.format(c.id),
                                           method='DELETE',
                                           data=json.dumps({'token': token}),
                                           headers=JSON_HEADER):
            res = self.app.full_dispatch_request()
            self.assertTrue(res.status_code == 400)
Пример #15
0
    def test_token_errors(self):
        u1 = User(email='*****@*****.**', username='******', password='******')
        u2 = User(email='*****@*****.**', username='******', password='******')
        t = Talk(title='t', description='d', author=u1)
        c = Comment(talk=t, body='c1', author_name='n',
                    author_email='*****@*****.**', approved=False)
        db.session.add_all([u1, u2, t, c])
        db.session.commit()

        # missing JSON --> 400
        with self.app.test_request_context(
                '/api/1.0/comments/' + str(c.id),
                method='PUT'):
            res = self.app.full_dispatch_request()
            self.assertTrue(res.status_code == 400)

        # missing token --> 401
        with self.app.test_request_context(
                '/api/1.0/comments/' + str(c.id),
                method='PUT',
                data=json.dumps({'bad': 123}),
                headers={'Content-Type': 'application/json'}):
            res = self.app.full_dispatch_request()
            self.assertTrue(res.status_code == 401)

        # bad token --> 401
        with self.app.test_request_context(
                '/api/1.0/comments/' + str(c.id),
                method='PUT',
                data=json.dumps({'token': 'a bad token'}),
                headers={'Content-Type': 'application/json'}):
            res = self.app.full_dispatch_request()
            self.assertTrue(res.status_code == 401)

        # malformed token --> 401
        u3 = User(email='*****@*****.**', username='******', password='******')
        with self.app.test_request_context(
                '/api/1.0/comments/' + str(c.id),
                method='PUT',
                data=json.dumps({'token': u3.get_api_token()}),
                headers={'Content-Type': 'application/json'}):
            res = self.app.full_dispatch_request()
            self.assertTrue(res.status_code == 401)
Пример #16
0
 def test_notification_list(self):
     db.create_all()
     u1 = User(email='*****@*****.**', username='******', password='******')
     u2 = User(email='*****@*****.**', username='******', password='******')
     t = Talk(title='t', description='d', author=u1)
     c1 = Comment(talk=t,
                  body='c1',
                  author_name='n1',
                  author_email='*****@*****.**',
                  approved=True)
     c2 = Comment(talk=t,
                  body='c2',
                  author_name='n2',
                  author_email='*****@*****.**',
                  approved=True,
                  notify=False)
     c3 = Comment(talk=t, body='c3', author=u2, approved=True)
     c4 = Comment(talk=t,
                  body='c4',
                  author_name='n4',
                  author_email='*****@*****.**',
                  approved=False)
     c5 = Comment(talk=t, body='c5', author=u2, approved=True)
     c6 = Comment(talk=t,
                  body='c6',
                  author_name='n6',
                  author_email='*****@*****.**',
                  approved=True,
                  notify=False)
     db.session.add_all([u1, u2, t, c1, c2, c3, c4, c5])
     db.session.commit()
     email_list = c4.notification_list()
     self.assertTrue(('*****@*****.**', 'n1') in email_list)
     self.assertFalse(('*****@*****.**', 'n2') in email_list)  # notify=False
     self.assertTrue(('*****@*****.**', 'susan') in email_list)
     self.assertFalse(('*****@*****.**', 'n4') in email_list)  # comment author
     self.assertFalse(('*****@*****.**', 'n6') in email_list)
     email_list = c5.notification_list()
     self.assertFalse(('*****@*****.**', 'john') in email_list)
     self.assertTrue(('*****@*****.**', 'n4') in email_list)  # comment author
Пример #17
0
def admin_talk_table():
    table = TalkTable(query=Talk.related_to(current_user))
    return table.get_response()
Пример #18
0
def listtalks():
    """List all talks """
    couchdb_manager.request_start()
    for talk in Talk.all(): 
        print(talk.title + " " + talk.description)
Пример #19
0
def addtalk(title, description):
    """Register a new talk."""
    couchdb_manager.request_start()
    talk = Talk(title = title, description = description)
    talk.store()
    print('Talk {0} was registered successfully.'.title)
Пример #20
0
def gettalk(id):
    """Get Talk by ID """
    couchdb_manager.request_start()
    talk = Talk.load(id)
    print(talk.title + " " + talk.description)