def test_form_creation_with_a_registered_email(self): httpretty.register_uri(httpretty.POST, 'https://api.sendgrid.com/api/mail.send.json') # register user r = self.client.post('/register', data={'email': '*****@*****.**', 'password': '******'} ) # upgrade user manually user = User.query.filter_by(email='*****@*****.**').first() user.upgraded = True DB.session.add(user) DB.session.commit() httpretty.reset() httpretty.register_uri(httpretty.POST, 'https://api.sendgrid.com/api/mail.send.json') # create form without providing an url should not send verification email r = self.client.post('/forms', headers={'Accept': 'application/json', 'Content-type': 'application/json'}, data=json.dumps({'email': '*****@*****.**'}) ) self.assertEqual(httpretty.has_request(), False) # create form without a confirmed email should send a verification email r = self.client.post('/forms', headers={'Accept': 'application/json', 'Content-type': 'application/json'}, data=json.dumps({'email': '*****@*****.**', 'url': 'https://www.testsite.com/contact.html'}) ) resp = json.loads(r.data) self.assertEqual(resp['confirmed'], False) self.assertEqual(httpretty.has_request(), True) self.assertIn('Confirm+email', httpretty.last_request().body) self.assertIn('www.testsite.com%2Fcontact.html', httpretty.last_request().body) # manually verify an email email = Email() email.address = '*****@*****.**' email.owner_id = user.id DB.session.add(email) DB.session.commit() # create a form with the verified email address r = self.client.post('/forms', headers={'Accept': 'application/json', 'Content-type': 'application/json'}, data=json.dumps({'email': '*****@*****.**', 'url': 'https://www.testsite.com/about.html'}) ) resp = json.loads(r.data) self.assertEqual(resp['confirmed'], True) self.assertIn('www.testsite.com%2Fcontact.html', httpretty.last_request().body) # same as the last, means no new request was made # should have three created forms in the end self.assertEqual(Form.query.count(), 3)
def test_form_creation_with_a_registered_email(client, msend): # register user r = client.post('/register', data={'email': '*****@*****.**', 'password': '******'}) # upgrade user manually user = User.query.filter_by(email='*****@*****.**').first() user.upgraded = True DB.session.add(user) DB.session.commit() # creating a form without providing an url should not send verification email msend.reset_mock() r = client.post('/forms', headers={'Accept': 'application/json', 'Content-type': 'application/json'}, data=json.dumps({'email': '*****@*****.**'}) ) assert not msend.called # create form without a confirmed email should send a verification email msend.reset_mock() r = client.post('/forms', headers={'Accept': 'application/json', 'Content-type': 'application/json'}, data=json.dumps({'email': '*****@*****.**', 'url': 'https://www.testsite.com/contact.html'}) ) resp = json.loads(r.data.decode('utf-8')) assert resp['confirmed'] == False assert msend.called assert 'Confirm email for' in msend.call_args[1]['subject'] assert 'www.testsite.com/contact.html' in msend.call_args[1]['text'] # manually verify an email email = Email() email.address = '*****@*****.**' email.owner_id = user.id DB.session.add(email) DB.session.commit() # create a form with the verified email address r = client.post('/forms', headers={'Accept': 'application/json', 'Content-type': 'application/json'}, data=json.dumps({'email': '*****@*****.**', 'url': 'https://www.testsite.com/about.html'}) ) resp = json.loads(r.data.decode('utf-8')) assert resp['confirmed'] == True assert 'www.testsite.com/contact.html' in msend.call_args[1]['text'] # same as the last, means no new request was made # should have three created forms in the end assert Form.query.count() == 3
def test_sitewide_forms(self): httpretty.register_uri(httpretty.GET, 'http://mysite.com/[email protected]', status=200) httpretty.register_uri(httpretty.GET, 'http://www.naive.com/[email protected]', status=200) # register user r = self.client.post('/register', data={'email': '*****@*****.**', 'password': '******'} ) # upgrade user manually user = User.query.filter_by(email='*****@*****.**').first() user.upgraded = True DB.session.add(user) DB.session.commit() # manually verify an email email = Email() email.address = '*****@*****.**' email.owner_id = user.id DB.session.add(email) DB.session.commit() # create a sitewide form with the verified email address r = self.client.post('/forms', headers={'Accept': 'application/json', 'Content-type': 'application/json'}, data=json.dumps({'email': '*****@*****.**', 'url': 'http://mysite.com', 'sitewide': 'true'}) ) resp = json.loads(r.data) self.assertEqual(httpretty.has_request(), True) self.assertEqual(resp['confirmed'], True) self.assertEqual(1, Form.query.count()) forms = Form.query.all() form = forms[0] self.assertEqual(form.sitewide, True) self.assertEqual(form.host, 'mysite.com') # submit form httpretty.register_uri(httpretty.POST, 'https://api.sendgrid.com/api/mail.send.json') r = self.client.post('/' + form.hashid, headers = {'Referer': 'http://www.mysite.com/hipopotamo', 'content-type': 'application/json'}, data=json.dumps({'name': 'alice'}) ) self.assertIn('alice', httpretty.last_request().body) self.client.post('/' + form.hashid, headers = {'Referer': 'http://mysite.com/baleia/urso?w=2', 'content-type': 'application/json'}, data=json.dumps({'name': 'maria'}) ) self.assertIn('maria', httpretty.last_request().body) self.client.post('/' + form.hashid, headers = {'Referer': 'http://mysite.com/', 'content-type': 'application/json'}, data=json.dumps({'name': 'laura'}) ) self.assertIn('laura', httpretty.last_request().body) # another form, now with a www prefix that will be stripped r = self.client.post('/forms', headers={'Accept': 'application/json', 'Content-type': 'application/json'}, data=json.dumps({'email': '*****@*****.**', 'url': 'http://www.naive.com', 'sitewide': 'true'}) ) resp = json.loads(r.data) self.assertEqual(httpretty.has_request(), True) self.assertEqual(resp['confirmed'], True) self.assertEqual(2, Form.query.count()) forms = Form.query.all() form = forms[1] self.assertEqual(form.sitewide, True) self.assertEqual(form.host, 'naive.com') # submit form httpretty.register_uri(httpretty.POST, 'https://api.sendgrid.com/api/mail.send.json') r = self.client.post('/' + form.hashid, headers = {'Referer': 'http://naive.com/hipopotamo', 'content-type': 'application/json'}, data=json.dumps({'name': 'alice'}) ) self.assertIn('alice', httpretty.last_request().body) self.client.post('/' + form.hashid, headers = {'Referer': 'http://www.naive.com/baleia/urso?w=2', 'content-type': 'application/json'}, data=json.dumps({'name': 'maria'}) ) self.assertIn('maria', httpretty.last_request().body) self.client.post('/' + form.hashid, headers = {'Referer': 'http://www.naive.com/', 'content-type': 'application/json'}, data=json.dumps({'name': 'laura'}) ) self.assertIn('laura', httpretty.last_request().body)
def test_sitewide_forms(self): httpretty.register_uri( httpretty.GET, 'http://mysite.com/formspree-verify.txt', body=u'[email protected]\nmyüñìćõð€[email protected]', status=200) httpretty.register_uri(httpretty.GET, 'http://www.naive.com/formspree-verify.txt', body=u'myüñìćõð€[email protected]', status=200) # register user r = self.client.post('/register', data={ 'email': '*****@*****.**', 'password': '******' }) # upgrade user manually user = User.query.filter_by(email='*****@*****.**').first() user.upgraded = True DB.session.add(user) DB.session.commit() # manually verify an email email = Email() email.address = u'myüñìćõð€[email protected]' email.owner_id = user.id DB.session.add(email) DB.session.commit() # create a sitewide form with the verified email address r = self.client.post('/forms', headers={ 'Accept': 'application/json', 'Content-type': 'application/json' }, data=json.dumps({ 'email': u'myüñìćõð€[email protected]', 'url': 'http://mysite.com', 'sitewide': 'true' })) resp = json.loads(r.data) self.assertEqual(httpretty.has_request(), True) self.assertEqual(resp['confirmed'], True) self.assertEqual(1, Form.query.count()) forms = Form.query.all() form = forms[0] self.assertEqual(form.sitewide, True) self.assertEqual(form.host, 'mysite.com') # submit form httpretty.register_uri(httpretty.POST, 'https://api.sendgrid.com/api/mail.send.json') r = self.client.post('/' + form.hashid, headers={ 'Referer': 'http://www.mysite.com/hipopotamo', 'content-type': 'application/json' }, data=json.dumps({'name': 'alice'})) self.assertIn('alice', httpretty.last_request().body) self.client.post('/' + form.hashid, headers={ 'Referer': 'http://mysite.com/baleia/urso?w=2', 'content-type': 'application/json' }, data=json.dumps({'name': 'maria'})) self.assertIn('maria', httpretty.last_request().body) self.client.post('/' + form.hashid, headers={ 'Referer': 'http://mysite.com/', 'content-type': 'application/json' }, data=json.dumps({'name': 'laura'})) self.assertIn('laura', httpretty.last_request().body) # another form, now with a www prefix that will be stripped r = self.client.post('/forms', headers={ 'Accept': 'application/json', 'Content-type': 'application/json' }, data=json.dumps({ 'email': u'myüñìćõð€[email protected]', 'url': 'http://www.naive.com', 'sitewide': 'true' })) resp = json.loads(r.data) self.assertEqual(httpretty.has_request(), True) self.assertEqual(resp['confirmed'], True) self.assertEqual(2, Form.query.count()) forms = Form.query.all() form = forms[1] self.assertEqual(form.sitewide, True) self.assertEqual(form.host, 'naive.com') # submit form httpretty.register_uri(httpretty.POST, 'https://api.sendgrid.com/api/mail.send.json') r = self.client.post('/' + form.hashid, headers={ 'Referer': 'http://naive.com/hipopotamo', 'content-type': 'application/json' }, data=json.dumps({'name': 'alice'})) self.assertIn('alice', httpretty.last_request().body) self.client.post('/' + form.hashid, headers={ 'Referer': 'http://www.naive.com/baleia/urso?w=2', 'content-type': 'application/json' }, data=json.dumps({'name': 'maria'})) self.assertIn('maria', httpretty.last_request().body) self.client.post('/' + form.hashid, headers={ 'Referer': 'http://www.naive.com/', 'content-type': 'application/json' }, data=json.dumps({'name': 'laura'})) self.assertIn('laura', httpretty.last_request().body) # create a different form with the same email address, now using unprefixed url r = self.client.post('/forms', headers={ 'Accept': 'application/json', 'Content-type': 'application/json' }, data=json.dumps({ 'email': u'myüñìćõð€[email protected]', 'url': 'mysite.com', 'sitewide': 'true' })) resp = json.loads(r.data)
def test_form_creation_with_a_registered_email(client, msend): # register user r = client.post( "/register", data={"email": "*****@*****.**", "password": "******"} ) # upgrade user manually user = User.query.filter_by(email="*****@*****.**").first() user.plan = Plan.gold DB.session.add(user) DB.session.commit() # creating a form without providing an url should not send verification email msend.reset_mock() r = client.post( "/api-int/forms", headers={ "Accept": "application/json", "Content-type": "application/json", "Referer": settings.SERVICE_URL, }, data=json.dumps({"email": "*****@*****.**"}), ) assert not msend.called # create form without a confirmed email should send a verification email msend.reset_mock() r = client.post( "/api-int/forms", headers={ "Accept": "application/json", "Content-type": "application/json", "Referer": settings.SERVICE_URL, }, data=json.dumps( { "email": "*****@*****.**", "url": "https://www.testsite.com/contact.html", } ), ) resp = json.loads(r.data.decode("utf-8")) assert resp["confirmed"] == False assert msend.called assert "Confirm email for" in msend.call_args[1]["subject"] assert "www.testsite.com/contact.html" in msend.call_args[1]["text"] # manually verify an email email = Email() email.address = "*****@*****.**" email.owner_id = user.id DB.session.add(email) DB.session.commit() # create a form with the verified email address r = client.post( "/api-int/forms", headers={ "Accept": "application/json", "Content-type": "application/json", "Referer": settings.SERVICE_URL, }, data=json.dumps( { "email": "*****@*****.**", "url": "https://www.testsite.com/about.html", } ), ) resp = json.loads(r.data.decode("utf-8")) assert resp["confirmed"] == True assert ( "www.testsite.com/contact.html" in msend.call_args[1]["text"] ) # same as the last, means no new request was made # should have three created forms in the end assert Form.query.count() == 3
def test_sitewide_forms(client, msend, mocker): m_sitewidecheck = mocker.patch( "formspree.forms.api.sitewide_file_check", side_effect=[True, True, True] ) # register user r = client.post( "/register", data={"email": "*****@*****.**", "password": "******"} ) # upgrade user manually user = User.query.filter_by(email="*****@*****.**").first() user.plan = Plan.gold DB.session.add(user) DB.session.commit() # manually verify an email email = Email() email.address = "myüñìćõð€[email protected]" email.owner_id = user.id DB.session.add(email) DB.session.commit() # create a sitewide form with the verified email address r = client.post( "/api-int/forms", headers={ "Accept": "application/json", "Content-type": "application/json", "Referer": settings.SERVICE_URL, }, data=json.dumps( { "email": "myüñìćõð€[email protected]", "url": "http://mysite.com", "sitewide": "true", } ), ) resp = json.loads(r.data.decode("utf-8")) assert m_sitewidecheck.called assert m_sitewidecheck.call_args[0][1] == "myüñìćõð€[email protected]" assert resp["confirmed"] m_sitewidecheck.reset_mock() assert 1 == Form.query.count() forms = Form.query.all() form = forms[0] assert form.sitewide assert form.host == "mysite.com" # submit form r = client.post( "/" + form.hashid, headers={ "Referer": "http://www.mysite.com/hipopotamo", "content-type": "application/json", }, data=json.dumps({"name": "alice"}), ) assert "alice" in msend.call_args[1]["text"] client.post( "/" + form.hashid, headers={ "Referer": "http://mysite.com/baleia/urso?w=2", "content-type": "application/json", }, data=json.dumps({"name": "maria"}), ) assert "maria" in msend.call_args[1]["text"] client.post( "/" + form.hashid, headers={"Referer": "http://mysite.com/", "content-type": "application/json"}, data=json.dumps({"name": "laura"}), ) assert "laura" in msend.call_args[1]["text"] # another form, now with a www prefix that will be stripped r = client.post( "/api-int/forms", headers={ "Accept": "application/json", "Content-type": "application/json", "Referer": settings.SERVICE_URL, }, data=json.dumps( { "email": "myüñìćõð€[email protected]", "url": "http://www.naive.com", "sitewide": "true", } ), ) resp = json.loads(r.data.decode("utf-8")) assert m_sitewidecheck.called assert m_sitewidecheck.call_args[0][0] == "http://www.naive.com" assert resp["confirmed"] assert 2 == Form.query.count() forms = Form.query.all() form = forms[1] assert form.sitewide assert form.host == "naive.com" # submit form r = client.post( "/" + form.hashid, headers={ "Referer": "http://naive.com/hipopotamo", "content-type": "application/json", }, data=json.dumps({"name": "alice"}), ) assert "alice" in msend.call_args[1]["text"] client.post( "/" + form.hashid, headers={ "Referer": "http://www.naive.com/baleia/urso?w=2", "content-type": "application/json", }, data=json.dumps({"name": "maria"}), ) assert "maria" in msend.call_args[1]["text"] client.post( "/" + form.hashid, headers={ "Referer": "http://www.naive.com/", "content-type": "application/json", }, data=json.dumps({"name": "laura"}), ) assert "laura" in msend.call_args[1]["text"] # create a different form with the same email address, now using unprefixed url r = client.post( "/api-int/forms", headers={ "Accept": "application/json", "Content-type": "application/json", "Referer": settings.SERVICE_URL, }, data=json.dumps( { "email": "myüñìćõð€[email protected]", "url": "mysite.com", "sitewide": "true", } ), ) resp = json.loads(r.data.decode("utf-8"))
def test_sitewide_forms(client, msend, mocker): m_sitewidecheck = mocker.patch( 'formspree.forms.views.sitewide_file_check', side_effect=[True, True, True] ) # register user r = client.post('/register', data={'email': '*****@*****.**', 'password': '******'} ) # upgrade user manually user = User.query.filter_by(email='*****@*****.**').first() user.upgraded = True DB.session.add(user) DB.session.commit() # manually verify an email email = Email() email.address = 'myüñìćõð€[email protected]' email.owner_id = user.id DB.session.add(email) DB.session.commit() # create a sitewide form with the verified email address r = client.post('/forms', headers={'Accept': 'application/json', 'Content-type': 'application/json'}, data=json.dumps({'email': 'myüñìćõð€[email protected]', 'url': 'http://mysite.com', 'sitewide': 'true'}) ) resp = json.loads(r.data.decode('utf-8')) assert m_sitewidecheck.called assert m_sitewidecheck.call_args[0][1] == 'myüñìćõð€[email protected]' assert resp['confirmed'] m_sitewidecheck.reset_mock() assert 1 == Form.query.count() forms = Form.query.all() form = forms[0] assert form.sitewide assert form.host == 'mysite.com' # submit form r = client.post('/' + form.hashid, headers = {'Referer': 'http://www.mysite.com/hipopotamo', 'content-type': 'application/json'}, data=json.dumps({'name': 'alice'}) ) assert 'alice' in msend.call_args[1]['text'] client.post('/' + form.hashid, headers = {'Referer': 'http://mysite.com/baleia/urso?w=2', 'content-type': 'application/json'}, data=json.dumps({'name': 'maria'}) ) assert 'maria' in msend.call_args[1]['text'] client.post('/' + form.hashid, headers = {'Referer': 'http://mysite.com/', 'content-type': 'application/json'}, data=json.dumps({'name': 'laura'}) ) assert 'laura' in msend.call_args[1]['text'] # another form, now with a www prefix that will be stripped r = client.post('/forms', headers={'Accept': 'application/json', 'Content-type': 'application/json'}, data=json.dumps({'email': 'myüñìćõð€[email protected]', 'url': 'http://www.naive.com', 'sitewide': 'true'}) ) resp = json.loads(r.data.decode('utf-8')) assert m_sitewidecheck.called assert m_sitewidecheck.call_args[0][0] == 'http://www.naive.com' assert resp['confirmed'] assert 2 == Form.query.count() forms = Form.query.all() form = forms[1] assert form.sitewide assert form.host == 'naive.com' # submit form r = client.post('/' + form.hashid, headers={'Referer': 'http://naive.com/hipopotamo', 'content-type': 'application/json'}, data=json.dumps({'name': 'alice'}) ) assert 'alice' in msend.call_args[1]['text'] client.post('/' + form.hashid, headers={'Referer': 'http://www.naive.com/baleia/urso?w=2', 'content-type': 'application/json'}, data=json.dumps({'name': 'maria'}) ) assert 'maria' in msend.call_args[1]['text'] client.post('/' + form.hashid, headers={'Referer': 'http://www.naive.com/', 'content-type': 'application/json'}, data=json.dumps({'name': 'laura'}) ) assert 'laura' in msend.call_args[1]['text'] # create a different form with the same email address, now using unprefixed url r = client.post('/forms', headers={'Accept': 'application/json', 'Content-type': 'application/json'}, data=json.dumps({'email': 'myüñìćõð€[email protected]', 'url': 'mysite.com', 'sitewide': 'true'}) ) resp = json.loads(r.data.decode('utf-8'))
def test_sitewide_forms(client, msend, mocker): m_sitewidecheck = mocker.patch("formspree.forms.api.sitewide_file_check", side_effect=[True, True, True]) # register user r = client.post("/register", data={ "email": "*****@*****.**", "password": "******" }) # upgrade user manually user = User.query.filter_by(email="*****@*****.**").first() user.plan = Plan.gold DB.session.add(user) DB.session.commit() # manually verify an email email = Email() email.address = "myüñìćõð€[email protected]" email.owner_id = user.id DB.session.add(email) DB.session.commit() # create a sitewide form with the verified email address r = client.post( "/api-int/forms", headers={ "Accept": "application/json", "Content-type": "application/json", "Referer": settings.SERVICE_URL, }, data=json.dumps({ "email": "myüñìćõð€[email protected]", "url": "http://mysite.com", "sitewide": "true", }), ) resp = json.loads(r.data.decode("utf-8")) assert m_sitewidecheck.called assert m_sitewidecheck.call_args[0][1] == "myüñìćõð€[email protected]" assert resp["confirmed"] m_sitewidecheck.reset_mock() assert 1 == Form.query.count() forms = Form.query.all() form = forms[0] assert form.sitewide assert form.host == "mysite.com" # submit form r = client.post( "/" + form.hashid, headers={ "Referer": "http://www.mysite.com/hipopotamo", "content-type": "application/json", }, data=json.dumps({"name": "alice"}), ) assert "alice" in msend.call_args[1]["text"] client.post( "/" + form.hashid, headers={ "Referer": "http://mysite.com/baleia/urso?w=2", "content-type": "application/json", }, data=json.dumps({"name": "maria"}), ) assert "maria" in msend.call_args[1]["text"] client.post( "/" + form.hashid, headers={ "Referer": "http://mysite.com/", "content-type": "application/json" }, data=json.dumps({"name": "laura"}), ) assert "laura" in msend.call_args[1]["text"] # another form, now with a www prefix that will be stripped r = client.post( "/api-int/forms", headers={ "Accept": "application/json", "Content-type": "application/json", "Referer": settings.SERVICE_URL, }, data=json.dumps({ "email": "myüñìćõð€[email protected]", "url": "http://www.naive.com", "sitewide": "true", }), ) resp = json.loads(r.data.decode("utf-8")) assert m_sitewidecheck.called assert m_sitewidecheck.call_args[0][0] == "http://www.naive.com" assert resp["confirmed"] assert 2 == Form.query.count() forms = Form.query.all() form = forms[1] assert form.sitewide assert form.host == "naive.com" # submit form r = client.post( "/" + form.hashid, headers={ "Referer": "http://naive.com/hipopotamo", "content-type": "application/json", }, data=json.dumps({"name": "alice"}), ) assert "alice" in msend.call_args[1]["text"] client.post( "/" + form.hashid, headers={ "Referer": "http://www.naive.com/baleia/urso?w=2", "content-type": "application/json", }, data=json.dumps({"name": "maria"}), ) assert "maria" in msend.call_args[1]["text"] client.post( "/" + form.hashid, headers={ "Referer": "http://www.naive.com/", "content-type": "application/json", }, data=json.dumps({"name": "laura"}), ) assert "laura" in msend.call_args[1]["text"] # create a different form with the same email address, now using unprefixed url r = client.post( "/api-int/forms", headers={ "Accept": "application/json", "Content-type": "application/json", "Referer": settings.SERVICE_URL, }, data=json.dumps({ "email": "myüñìćõð€[email protected]", "url": "mysite.com", "sitewide": "true", }), ) resp = json.loads(r.data.decode("utf-8"))
def test_form_creation_with_a_registered_email(client, msend): # register user r = client.post("/register", data={ "email": "*****@*****.**", "password": "******" }) # upgrade user manually user = User.query.filter_by(email="*****@*****.**").first() user.plan = Plan.gold DB.session.add(user) DB.session.commit() # creating a form without providing an url should not send verification email msend.reset_mock() r = client.post( "/api-int/forms", headers={ "Accept": "application/json", "Content-type": "application/json", "Referer": settings.SERVICE_URL, }, data=json.dumps({"email": "*****@*****.**"}), ) assert not msend.called # create form without a confirmed email should send a verification email msend.reset_mock() r = client.post( "/api-int/forms", headers={ "Accept": "application/json", "Content-type": "application/json", "Referer": settings.SERVICE_URL, }, data=json.dumps({ "email": "*****@*****.**", "url": "https://www.testsite.com/contact.html", }), ) resp = json.loads(r.data.decode("utf-8")) assert resp["confirmed"] == False assert msend.called assert "Confirm email for" in msend.call_args[1]["subject"] assert "www.testsite.com/contact.html" in msend.call_args[1]["text"] # manually verify an email email = Email() email.address = "*****@*****.**" email.owner_id = user.id DB.session.add(email) DB.session.commit() # create a form with the verified email address r = client.post( "/api-int/forms", headers={ "Accept": "application/json", "Content-type": "application/json", "Referer": settings.SERVICE_URL, }, data=json.dumps({ "email": "*****@*****.**", "url": "https://www.testsite.com/about.html", }), ) resp = json.loads(r.data.decode("utf-8")) assert resp["confirmed"] == True assert ("www.testsite.com/contact.html" in msend.call_args[1]["text"] ) # same as the last, means no new request was made # should have three created forms in the end assert Form.query.count() == 3