def post_body_locale_determined_by_import( request, test_session, discussion, admin_user, mailbox, undefined_locale, fr_locale, en_locale): from assembl.models import Email, LangString p = Email( discussion=discussion, creator=admin_user, subject=LangString.create(u"testa"), body=LangString.create(u"testa"), source=mailbox, body_mime_type="text/plain", sender="*****@*****.**", recipients="*****@*****.**", message_id="*****@*****.**", imported_blob="""Subject: testa From: Mr. Administrator <*****@*****.**> Content-Language: fr Content-Type: text/plain; charset="iso-8859-1" testa""") # must be done after the source is set p.source_post_id = "*****@*****.**" test_session.add(p) test_session.flush() def fin(): print "finalizer post_subject_locale_determined_by_creator" test_session.delete(p) test_session.flush() request.addfinalizer(fin) return p
def upgrade(pyramid_env): with context.begin_transaction(): op.add_column('post', sa.Column( u'creator_id', sa.Integer, sa.ForeignKey('agent_profile.id'))) SQLAlchemyBaseModel.metadata.bind = op.get_bind() # Do stuff with the app's models here. from assembl.models import Email, EmailAccount db = Email.db() with transaction.manager: for mail in db.query(Email).all(): sender_name, sender_email = parseaddr(mail.sender) account = EmailAccount.get_or_make_profile( db, sender_email, sender_name) mail.post.creator = account.profile
def test_api_get_posts_from_idea( discussion, test_app, test_session, participant1_user, root_idea, subidea_1, subidea_1_1, subidea_1_1_1, root_post_1, reply_post_1, reply_post_2): base_post_url = get_url(discussion, 'posts') base_idea_url = get_url(discussion, 'ideas') base_extract_url = get_url(discussion, 'extracts') #Check initial conditions from post api url = base_post_url res = test_app.get(url) assert res.status_code == 200 res_data = json.loads(res.body) assert res_data['total'] == 3 #Check initial conditions from idea api res = test_app.get(base_idea_url + "/" + str(root_idea.id)) assert res.status_code == 200 res_data = json.loads(res.body) assert res_data['num_posts'] == 3 assert res_data['num_orphan_posts'] == 3 def check_number_of_posts(idea, expected_num, fail_msg): #Check from idea API idea.db.flush() res = test_app.get(base_idea_url + "/" + str(idea.id)) assert res.status_code == 200 res_data = json.loads(res.body) assert res_data['num_posts'] == expected_num, "idea API returned %d but %s" % (res_data['num_posts'],fail_msg) url = base_post_url + "?" + urlencode({"root_idea_id": idea.uri()}) res = test_app.get(url) assert res.status_code == 200 res_data = json.loads(res.body) #print(repr(res_data)) #TODO: BENOITG: THERE IS A SESSION PROBLEM HERE assert res_data['total'] == expected_num, "post API returned %d but %s" % (res_data['total'],fail_msg) def check_total_and_orphans(expected_total, expected_orphans): #Check orphans from idea api res = test_app.get(base_idea_url + "/" + str(root_idea.id)) assert res.status_code == 200 res_data = json.loads(res.body) assert res_data['num_posts'] == expected_total # Known to fail. I get 0 on v6, ? on v7. assert res_data['num_orphan_posts'] == expected_orphans check_number_of_posts(subidea_1, 0, "Initially no posts are linked") check_number_of_posts(subidea_1_1, 0, "Initially no posts are linked") check_number_of_posts(subidea_1_1_1, 0, "Initially no posts are linked") user = participant1_user extract_user = { "@id": user.uri(), "name": user.name, "@type": User.external_typename()} base_extract_data = { "idIdea": None, "creator": extract_user, "owner": extract_user, "text": "Let's lower taxes to fav", "creationDate": 1376573216160, "target": { "@type": Email.external_typename(), "@id": None } } #Create extract extract_data = base_extract_data.copy() extract_data["idIdea"] = subidea_1_1.uri() extract_data["target"]['@id'] = reply_post_1.uri() res = test_app.post(base_extract_url, json.dumps(extract_data)) assert res.status_code == 200 res_data = json.loads(res.body) extract_post_1_to_subidea_1_1_id = res_data['@id'] #test_session.flush() #test_session.expunge_all() check_number_of_posts(subidea_1_1, 2, "Num posts on idea (directly) should recurse to the two posts") #import transaction #transaction.commit() check_number_of_posts(subidea_1, 2, "Num posts on parent idea should be the same as the child") check_number_of_posts(subidea_1_1_1, 0, "Num posts on child of idea should still be zero") check_total_and_orphans(3, 1) #Create second extract to same post and idea extract_data = base_extract_data.copy() extract_data["idIdea"] = subidea_1_1.uri() extract_data["target"]['@id'] = reply_post_1.uri() extract_data["text"] = "Let's lower taxes to fav 2", res = test_app.post(base_extract_url, json.dumps(extract_data)) assert res.status_code == 200 res_data = json.loads(res.body) extract_post_1_to_subidea_1_1_bis_id = res_data['@id'] check_number_of_posts(subidea_1_1, 2, "Num posts should not have changed with second identical extract") check_number_of_posts(subidea_1, 2, "Num posts on parent idea should not have changed with second identical extract") check_total_and_orphans(3, 1) #Create extract from parent idea to leaf message extract_data = base_extract_data.copy() extract_data["idIdea"] = subidea_1.uri() extract_data["target"]['@id'] = reply_post_2.uri() extract_data["text"] = "Let's lower taxes to fav 3", res = test_app.post(base_extract_url, json.dumps(extract_data)) assert res.status_code == 200 res_data = json.loads(res.body) extract_post_2_to_subidea_1_id = res_data['@id'] check_number_of_posts(subidea_1_1, 2, "Child idea should still have two posts") check_number_of_posts(subidea_1, 2, "Idea should still have two posts") check_number_of_posts(subidea_1_1_1, 0, "Num posts on leaf idea should still be zero") check_total_and_orphans(3, 1) #Delete original extract and duplicate (check that toombstones have no effect res = test_app.delete(base_extract_url + "/" + quote_plus(extract_post_1_to_subidea_1_1_id)) assert res.status_code == 204 res = test_app.delete(base_extract_url + "/" + quote_plus(extract_post_1_to_subidea_1_1_bis_id)) assert res.status_code == 204 check_number_of_posts(subidea_1_1, 0, "Child idea should no longer have any post") check_number_of_posts(subidea_1, 1, "Parent idea should only have one post left") check_number_of_posts(subidea_1_1_1, 0, "Num posts on leaf idea should still be zero") check_total_and_orphans(3, 2)
def test_extracts( discussion, participant1_user, reply_post_2, test_app, subidea_1_1, extract_post_1_to_subidea_1_1): from assembl.models import Extract user = participant1_user extract_user = { "@id": user.uri(), "name": user.name, "@type": User.external_typename()} extract_data = { "idIdea": None, "creator": extract_user, "owner": extract_user, "text": "Let's lower taxes to fav", "creationDate": 1376573216160, "target": { "@type": Email.external_typename(), "@id": reply_post_2.uri() } } base_url = get_url(discussion, 'extracts') #Load collection res = test_app.get(base_url) assert res.status_code == 200 extracts = json.loads(res.body) assert len(extracts) == 1 #Load extract directly res = test_app.get(base_url + "/" + quote_plus(extract_post_1_to_subidea_1_1.uri())) assert res.status_code == 200 extract_json = json.loads(res.body) assert extract_json['@id'] == extract_post_1_to_subidea_1_1.uri() #Check the API returns a 404 for extracts that never existed res = test_app.get(base_url + "/" + quote_plus("id_that_does_not_exist"), expect_errors=True) assert res.status_code == 404 #Create (Post) res = test_app.post(base_url, json.dumps(extract_data)) assert res.status_code == 200 res_data = json.loads(res.body) extract_id = res_data['@id'] assert extract_id != None #Check collection res = test_app.get(base_url, json.dumps(extract_data)) assert res.status_code == 200 extracts = json.loads(res.body) assert len(extracts) == 2 assert extract_id in [e['@id'] for e in extracts] #Update (PUT) #TODO: We should test this field by field url = base_url + "/" + quote_plus(extract_id) modified_extract_data = extract_data.copy() modified_extract_data["idIdea"] = subidea_1_1.uri() res = test_app.put(url, json.dumps(modified_extract_data)) assert res.status_code == 200 Extract.db.flush() res = test_app.get(url) assert res.status_code == 200 extract_json = json.loads(res.body) assert extract_json['idIdea'] == subidea_1_1.uri() #Delete res = test_app.delete(base_url + "/" + quote_plus(extract_id)) assert res.status_code == 204 #Check collection after delete res = test_app.get(base_url, json.dumps(extract_data)) assert res.status_code == 200 extracts = json.loads(res.body) assert len(extracts) == 1 assert extract_id not in [e['@id'] for e in extracts] #FIXME: This should actually return 410 gone now res = test_app.get(base_url + "/" + quote_plus(extract_id), expect_errors=True) assert res.status_code == 404
def test_api_get_posts_from_idea(discussion, test_app, test_session, participant1_user, root_idea, subidea_1, subidea_1_1, subidea_1_1_1, root_post_1, reply_post_1, reply_post_2): base_post_url = get_url(discussion, 'posts') base_idea_url = get_url(discussion, 'ideas') base_extract_url = get_url(discussion, 'extracts') # Check initial conditions from post api url = base_post_url res = test_app.get(url) assert res.status_code == 200 res_data = json.loads(res.body) assert res_data['total'] == 3 # Check initial conditions from idea api res = test_app.get(base_idea_url + "/" + str(root_idea.id)) assert res.status_code == 200 res_data = json.loads(res.body) assert res_data['num_total_and_read_posts'][0] == 3 assert res_data['num_orphan_posts'] == 3 def check_number_of_posts(idea, expected_num, fail_msg): # Check from idea API discussion.db.flush() res = test_app.get(base_idea_url + "/" + str(idea.id)) assert res.status_code == 200 res_data = json.loads(res.body) assert res_data['num_total_and_read_posts'][ 0] == expected_num, "idea API returned %d but %s" % ( res_data['num_total_and_read_posts'][0], fail_msg) url = base_post_url + "?" + urlencode({"root_idea_id": idea.uri()}) res = test_app.get(url) assert res.status_code == 200 res_data = json.loads(res.body) # print(repr(res_data)) # TODO: BENOITG: THERE IS A SESSION PROBLEM HERE assert res_data[ 'total'] == expected_num, "post API returned %d but %s" % ( res_data['total'], fail_msg) def check_total_and_orphans(expected_total, expected_orphans): # Check orphans from idea api res = test_app.get(base_idea_url + "/" + str(root_idea.id)) assert res.status_code == 200 res_data = json.loads(res.body) assert res_data['num_total_and_read_posts'][0] == expected_total # Known to fail. I get 0 on v6, ? on v7. assert res_data['num_orphan_posts'] == expected_orphans check_number_of_posts(subidea_1, 0, "Initially no posts are linked") check_number_of_posts(subidea_1_1, 0, "Initially no posts are linked") check_number_of_posts(subidea_1_1_1, 0, "Initially no posts are linked") user = participant1_user extract_user = { "@id": user.uri(), "name": user.name, "@type": User.external_typename() } base_extract_data = { "idIdea": None, "creator": extract_user, "owner": extract_user, "text": "Let's lower taxes to fav", "creationDate": 1376573216160, "target": { "@type": Email.external_typename(), "@id": None } } # Create extract extract_data = base_extract_data.copy() extract_data["idIdea"] = subidea_1_1.uri() extract_data["target"]['@id'] = reply_post_1.uri() res = test_app.post(base_extract_url, json.dumps(extract_data)) assert res.status_code == 200 res_data = json.loads(res.body) extract_post_1_to_subidea_1_1_id = res_data['@id'] # test_session.flush() check_number_of_posts( subidea_1_1, 2, "Num posts on idea (directly) should recurse to the two posts") # import transaction # transaction.commit() check_number_of_posts( subidea_1, 2, "Num posts on parent idea should be the same as the child") check_number_of_posts(subidea_1_1_1, 0, "Num posts on child of idea should still be zero") check_total_and_orphans(3, 1) # Create second extract to same post and idea extract_data = base_extract_data.copy() extract_data["idIdea"] = subidea_1_1.uri() extract_data["target"]['@id'] = reply_post_1.uri() extract_data["text"] = "Let's lower taxes to fav 2", res = test_app.post(base_extract_url, json.dumps(extract_data)) assert res.status_code == 200 res_data = json.loads(res.body) extract_post_1_to_subidea_1_1_bis_id = res_data['@id'] check_number_of_posts( subidea_1_1, 2, "Num posts should not have changed with second identical extract") check_number_of_posts( subidea_1, 2, "Num posts on parent idea should not have changed " + "with second identical extract") check_total_and_orphans(3, 1) # Create extract from parent idea to leaf message extract_data = base_extract_data.copy() extract_data["idIdea"] = subidea_1.uri() extract_data["target"]['@id'] = reply_post_2.uri() extract_data["text"] = "Let's lower taxes to fav 3", res = test_app.post(base_extract_url, json.dumps(extract_data)) assert res.status_code == 200 res_data = json.loads(res.body) extract_post_2_to_subidea_1_id = res_data['@id'] check_number_of_posts(subidea_1_1, 2, "Child idea should still have two posts") check_number_of_posts(subidea_1, 2, "Idea should still have two posts") check_number_of_posts(subidea_1_1_1, 0, "Num posts on leaf idea should still be zero") check_total_and_orphans(3, 1) # Delete original extract and duplicate # (check that toombstones have no effect) res = test_app.delete(base_extract_url + "/" + quote_plus(extract_post_1_to_subidea_1_1_id)) assert res.status_code == 204 res = test_app.delete(base_extract_url + "/" + quote_plus(extract_post_1_to_subidea_1_1_bis_id)) assert res.status_code == 204 check_number_of_posts(subidea_1_1, 0, "Child idea should no longer have any post") check_number_of_posts(subidea_1, 1, "Parent idea should only have one post left") check_number_of_posts(subidea_1_1_1, 0, "Num posts on leaf idea should still be zero") check_total_and_orphans(3, 2)
def test_extracts(discussion, participant1_user, reply_post_2, test_app, subidea_1_1, extract_post_1_to_subidea_1_1): from assembl.models import Extract user = participant1_user extract_user = { "@id": user.uri(), "name": user.name, "@type": User.external_typename() } extract_data = { "idIdea": None, "creator": extract_user, "owner": extract_user, "text": "Let's lower taxes to fav", "creationDate": 1376573216160, "target": { "@type": Email.external_typename(), "@id": reply_post_2.uri() } } base_url = get_url(discussion, 'extracts') #Load collection res = test_app.get(base_url) assert res.status_code == 200 extracts = json.loads(res.body) assert len(extracts) == 1 #Load extract directly res = test_app.get(base_url + "/" + quote_plus(extract_post_1_to_subidea_1_1.uri())) assert res.status_code == 200 extract_json = json.loads(res.body) assert extract_json['@id'] == extract_post_1_to_subidea_1_1.uri() #Check the API returns a 404 for extracts that never existed res = test_app.get(base_url + "/" + quote_plus("id_that_does_not_exist"), expect_errors=True) assert res.status_code == 404 #Create (Post) res = test_app.post(base_url, json.dumps(extract_data)) assert res.status_code == 200 res_data = json.loads(res.body) extract_id = res_data['@id'] assert extract_id != None #Check collection res = test_app.get(base_url, json.dumps(extract_data)) assert res.status_code == 200 extracts = json.loads(res.body) assert len(extracts) == 2 assert extract_id in [e['@id'] for e in extracts] #Update (PUT) #TODO: We should test this field by field url = base_url + "/" + quote_plus(extract_id) modified_extract_data = extract_data.copy() modified_extract_data["idIdea"] = subidea_1_1.uri() res = test_app.put(url, json.dumps(modified_extract_data)) assert res.status_code == 200 discussion.db.flush() res = test_app.get(url) assert res.status_code == 200 extract_json = json.loads(res.body) assert extract_json['idIdea'] == subidea_1_1.uri() # Delete res = test_app.delete(base_url + "/" + quote_plus(extract_id)) assert res.status_code == 204 # Check collection after delete res = test_app.get(base_url, json.dumps(extract_data)) assert res.status_code == 200 extracts = json.loads(res.body) assert len(extracts) == 1 assert extract_id not in [e['@id'] for e in extracts] # FIXME: This should actually return 410 gone now res = test_app.get(base_url + "/" + quote_plus(extract_id), expect_errors=True) assert res.status_code == 404