def test_should_update_value_of_feature_state(self): # Given client = self.set_up() project = Project.objects.get(name="test project") feature = Feature(name="feature", project=project) feature.save() environment = Environment.objects.get(name="test env") feature_state = FeatureState.objects.get(feature=feature, environment=environment) # When response = client.put( "/api/v1/environments/%s/featurestates/%d/" % (environment.api_key, feature_state.id), data=self.fs_put_template % (feature_state.id, True, "This is a value"), content_type='application/json') # should change enabled to True # Then self.assertEquals(response.status_code, status.HTTP_200_OK) feature_state.refresh_from_db() self.assertEquals(feature_state.get_feature_state_value(), "This is a value") self.assertEquals(feature_state.enabled, True) Helper.clean_up()
def test_add_to_cart_when_not_logged_in(self): user = User.objects.create_user(username='******', password='******') feature = Feature(featureName='Test Feature', author=user) feature.save() page = self.client.post('/cart/add/{0}'.format(feature.id), {'contribution': 100}, follow=True) self.assertEqual(page.status_code, 200) self.assertTemplateUsed(page, 'login.html')
def test_should_change_enabled_state_when_put(self): # Given client = self.set_up() organisation = Organisation.objects.get(name="test org") project = Project.objects.get(name="test project", organisation=organisation) feature = Feature(name='feature1', project=project) feature.save() environment = Environment.objects.get(name="test env") identity = Identity(identifier="test_identity", environment=environment) identity.save() feature_state = FeatureState(feature=feature, identity=identity, enabled=False, environment=environment) feature_state.save() # When response = client.put(self.feature_states_detail_url % (identity.environment.api_key, identity.identifier, feature_state.id), data=self.put_template % True, content_type='application/json') feature_state.refresh_from_db() # Then self.assertEquals(response.status_code, status.HTTP_200_OK) self.assertEquals(feature_state.enabled, True) Helper.clean_up()
def test_should_return_BadRequest_when_duplicate_identityFeature_is_posted( self): # Given client = self.set_up() identity, project = Helper.generate_database_models(self.identifier) feature = Feature(name='feature2', project=project) feature.save() # When initialResponse = client.post( self.feature_states_url % (identity.environment.api_key, identity.identifier), data=self.post_template % (feature.id, True), content_type='application/json') secondResponse = client.post( self.feature_states_url % (identity.environment.api_key, identity.identifier), data=self.post_template % (feature.id, True), content_type='application/json') # Then identityFeature = identity.identity_features self.assertEquals(initialResponse.status_code, status.HTTP_201_CREATED) self.assertEquals(secondResponse.status_code, status.HTTP_400_BAD_REQUEST) self.assertEquals(identityFeature.count(), 1) Helper.clean_up()
def test_should_remove_identityfeature_when_delete(self): # Given client = self.set_up() organisation = Organisation.objects.get(name="test org") project = Project.objects.get(name="test project", organisation=organisation) feature_one = Feature(name='feature1', project=project) feature_one.save() feature_two = Feature(name='feature2', project=project) feature_two.save() environment = Environment.objects.get(name="test env") identity = Identity(identifier="test_identity", environment=environment) identity.save() environment = Environment.objects.get(name="test env") identity_feature_one = FeatureState(feature=feature_one, identity=identity, enabled=False, environment=environment) identity_feature_one.save() identity_feature_two = FeatureState(feature=feature_two, identity=identity, enabled=True, environment=environment) identity_feature_two.save() # When client.delete(self.feature_states_detail_url % (identity.environment.api_key, identity.identifier, identity_feature_one.id), content_type='application/json') # Then identity_features = FeatureState.objects.filter(identity=identity) self.assertEquals(identity_features.count(), 1) Helper.clean_up()
def test_checkout_invalidForm(self): user = User.objects.create_user(username='******', password='******') self.client.login(username='******', password='******') feature = Feature(featureName='Test Feature', author=user) feature.save() page = self.client.post('/cart/add/{0}'.format(feature.id), {'contribution': 10}, follow=True) stripe_id = 'tok_chargeDeclined' page = self.client.post('/checkout/', { 'phone_number': '1234', 'street_address1': 'my', 'street_address2': 'address', 'town_or_city': 'xx', 'province': 'ireland', 'country': 'ireland', 'postcode': 'testcode', 'credit_card_number': '4000400040004000', 'cvv': '111', 'expiry_month': '2', 'expiry_year': '2020', 'stripe_id': stripe_id }, follow=True) messages = list(page.context['messages']) self.assertEqual(len(messages), 1) self.assertEqual(str(messages[0]), 'We were unable to take a payment with that card!') self.assertEqual(page.status_code, 200) self.assertTemplateUsed('checkout.html')
def test_checkout_correct_creditcard_details(self): user = User.objects.create_user(username='******', password='******') self.client.login(username='******', password='******') feature = Feature(featureName='Test Feature', author=user) feature.save() page = self.client.post('/cart/add/{0}'.format(feature.id), {'contribution': 10}, follow=True) stripe_id = 'tok_visa' page = self.client.post('/checkout/', { 'full_name': 'name', 'phone_number': '1234', 'street_address1': 'my', 'street_address2': 'address', 'town_or_city': 'xx', 'province': 'ireland', 'country': 'ireland', 'postcode': 'testcode', 'credit_card_number': '4242424242424242', 'cvv': '111', 'expiry_month': '2', 'expiry_year': '2020', 'stripe_id': stripe_id }, follow=True) messages = list(page.context['messages']) self.assertEqual(len(messages), 1) self.assertEqual(str(messages[0]), 'Thank you for your contribution') self.assertEqual(page.status_code, 200) self.assertTemplateUsed('features.html')
def test_checkout_with_login_card_declined(self): self.client.login(username='******', password='******') item = Feature(name="Test feature", description="description of my test feature", donations=30.00, username=self.user) item.save() self.client.post("/cart/add/{0}".format(item.id), data={ 'donation_amount': '12', 'user': self.user }) stripe_id = 'tok_chargeDeclined' response = self.client.post( "/checkout/", { 'user': self.user, 'credit_card_number': '4000000000000002', 'cvv': '123', 'expiry_month': '04', 'expiry_year': '2024', 'stripe_id': stripe_id }) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, "checkout.html") message = list(response.context.get('messages'))[0] self.assertEqual(message.tags, "error") self.assertEqual( message.message, "Sorry, we were unable to take a payment with that card")
def test_checkout_with_login_post(self): self.client.login(username='******', password='******') item = Feature(name="Test feature", description="description of my test feature", donations=30.00, username=self.user) item.save() # create shopping cart self.client.post("/cart/add/{0}".format(item.id), data={ 'donation_amount': '6', 'user': self.user }) stripe_id = 'tok_gb' response = self.client.post("/checkout/", { 'user': self.user, 'credit_card_number': '4242424242424242', 'cvv': '123', 'expiry_month': '10', 'expiry_year': '2024', 'stripe_id': stripe_id }, follow=True) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, "features.html") message = list(response.context.get('messages'))[0] self.assertEqual(message.tags, "success") self.assertEqual(message.message, "Thank you. You have successfully donated")
def test_adjust_cart_when_contribution_is_zero(self): user = User.objects.create_user(username='******', password='******') login = self.client.login(username='******', password='******') self.assertTrue(login) feature = Feature(featureName='Test Feature', author=user) feature.save() page = self.client.post('/cart/adjust/{0}'.format(feature.id), {'contribution': 100}, follow=True) page = self.client.post('/cart/adjust/{0}'.format(feature.id), {'contribution': 0}, follow=True) self.assertEqual(page.status_code, 200) self.assertTemplateUsed(page, 'cart.html')
def test_load_page(self): user = User.objects.create_user('username', '*****@*****.**','password') feature = Feature(title="Test Feature", author=user, content="Test Content") feature.save() self.client.login(username='******', password='******') self.client.post('/cart/add/{0}'.format(feature.id), {'contribution': '50'}, follow=True) stripe_id = 'tok_visa' page=self.client.get('/checkout/') self.assertTemplateUsed('checkout/checkout.html')
def test_add_to_cart(self): """Test that we can add items to our cart""" user = User(username="******") user.save() feature = Feature(title="test", description="testing", creator=user) feature.save() page = self.client.get('/cart/add/{0}'.format(feature.id)) self.assertEqual(page.status_code, 301)
def test_get_feature_detail_page(self): """ Testing single feature detail view """ user = User.objects.create_user(username='******', password='******') user.save() feature = Feature(name='Feature', description='Testing', user=user, price=20, upvotes=0, status='INCOMPLETE') feature.save() page = self.client.get('/features/feature_detail/1', follow=True) self.assertEqual(page.status_code, 200) self.assertTemplateUsed(page, "feature_detail.html")
def test_checkout_invalid_order_form(self): user = User.objects.create_user('username', '*****@*****.**','password') feature = Feature(title="Test Feature", author=user, content="Test Content") feature.save() self.client.login(username='******', password='******') self.client.post('/cart/add/{0}'.format(feature.id), {'contribution': '50'}, follow=True) stripe_id = 'tok_chargeDeclined' page = self.client.post('/checkout/', {'phone_number':'123', 'street_address1':'my', 'street_address2':'address is', 'town_or_city':'kk', 'county':'ireland', 'country':'ireland','postcode':'eircode', 'credit_card_number': '4000400040004000','cvv':'111', 'expiry_month':'2','expiry_year':'2019', 'stripe_id':stripe_id} , follow=True) messages = list(get_messages(page.wsgi_request)) self.assertEqual(str(messages[0]), 'We were unable to take a payment with that card!')
def generate_database_models(identifier='user1'): organisation = Organisation(name='ssg') organisation.save() project = Project(name='project1', organisation=organisation) project.save() environment = Environment(name='environment1', project=project) environment.save() feature = Feature(name="feature1", project=project) feature.save() identity = Identity(identifier=identifier, environment=environment) identity.save() return identity, project
def test_cannot_create_feature_with_same_case_insensitive_name(self): # Given feature_name = 'Test Feature' feature_one = Feature(project=self.project, name=feature_name) feature_two = Feature(project=self.project, name=feature_name.lower()) # When feature_one.save() # Then with pytest.raises(IntegrityError): feature_two.save()
def test_checkout_with_correct_card_details(self): user = User.objects.create_user('username', '*****@*****.**','password') feature = Feature(title="Test Feature", author=user, content="Test Content") feature.save() self.client.login(username='******', password='******') self.client.post('/cart/add/{0}'.format(feature.id), {'contribution': '50'}, follow=True) stripe_id = 'tok_visa' page = self.client.post('/checkout/', {'full_name':'name','phone_number':'123', 'street_address1':'my', 'street_address2':'address is', 'town_or_city':'kk', 'county':'ireland', 'country':'ireland','postcode':'eircode', 'credit_card_number': '4242424242424242','cvv':'111', 'expiry_month':'2','expiry_year':'2019', 'stripe_id':stripe_id}, follow=True) self.assertEqual(page.status_code, 200) messages = list(page.context['messages']) self.assertEqual(len(messages), 1) self.assertTemplateUsed('features/feature_detail.html') self.assertEqual(str(messages[0]), 'You have successfully contributed')
def test_checkout_payment_with_valid_credentials(self): feature = Feature(title="Test Feature", description="desc") feature.save() feature = Feature.objects.get(id=1) page = self.client.post( '/checkout/', { 'credit_card_number': '4242424242424242', 'cvv': '123', 'expiry_month': '8', 'expiry_year': '2025', 'stripe_id': 'tok_visa', }) self.assertTrue(page.status_code, 200)
def test_checkout_with_login_card_declined(self): client = Client() self.user = User.objects.create_user('john10', '*****@*****.**', 'john10') self.user.save() self.client.login(username="******", password="******") item = Feature(name="Create a Test feature", description="test description", price=30.00, author=self.user) item.save() self.client.post("/cart/add/{0}".format(item.id), data={ 'quantity': '6', 'author': self.user }, follow=True) # assign the stripe publishable key to stripe_id #tok_chargeDeclined is from stripe website, will give a card declined error stripe_id = 'tok_chargeDeclined' page = self.client.post("/checkout/", { 'full_name': 'john', 'phone_number': '123456789111', 'street_address1': 'home', 'street_address2': 'and dry', 'town_or_city': 'bay', 'county': 'dublin', 'country': 'ireland', 'postcode': 'eircode', 'credit_card_number': '4000000000000002', 'cvv': '111', 'expiry_month': '6', 'expiry_year': '2020', 'stripe_id': stripe_id }, follow=True) #check after post, that redirect was call '302' self.assertEqual(page.status_code, 200) self.assertTemplateUsed(page, "checkout.html") #check error message message = list(page.context.get('messages'))[0] self.assertEqual(message.tags, "error") self.assertEqual(message.message, "Your card was declined!")
def handle(self, *args, **options): with open(FILE_NAME, 'r') as features: features.next() # skip header reader = csv.reader(features, delimiter='\t') for row in reader: try: teams = self.get_team_objects(filter(None, row[TEAMS].split(';'))) if row[FEATURE]: feature = Feature(name=row[FEATURE], theme=row[THEME], clarity_or_jira_id=row[ID], url=row[URL]) feature.save() feature.teams.add(*teams) feature.save() except Exception as e: print('Error importing {} - {}'.format(row, e))
def test_checkout_payment_with_invalid_credentials(self): feature = Feature(title="Test Feature", description="desc") feature.save() feature = Feature.objects.get(id=1) page = self.client.post( '/checkout/', { 'credit_card_number': '4242424242424242', 'cvv': '123', 'expiry_month': '8', 'expiry_year': '2025', 'stripe_id': 'tok_chargeDeclined', }) # https://stackoverflow.com/questions/2897609/how-can-i-unit-test-django-messages messages = list(get_messages(page.wsgi_request)) self.assertEqual(str(messages[0]), 'We were unable to take a payment with that card!')
def test_add_to_cart(self): """ Test to see if an item is added to cart """ admin = User(username="******") admin.save() feature = Feature(name="test", description="test", status="done", upvotes=0, views=0, author=admin) feature.save() page = self.client.get("/cart/add{0}".format(feature.id)) session = self.client.session cart = session['cart'] self.assertEqual(cart, {'1': 1})
def test_creation(self): self.assertEqual(Feature.objects.count(), 0) f = Feature( title='Test title', description='Test description.', site=self.site, ) f.save() self.assertEqual(Feature.objects.count(), 1) f2 = Feature.objects.create( title='Another feature', description='Test description again.', site=self.site, ) self.assertEqual(Feature.objects.count(), 2)
def test_should_create_identityFeature_when_post(self): # Given client = self.set_up() environment = Environment.objects.get(name="test env") identity = Identity.objects.create(environment=environment, identifier="testidentity") project = Project.objects.get(name="test project") feature = Feature(name='feature1', project=project) feature.save() # When response = client.post( self.feature_states_url % (identity.environment.api_key, identity.identifier), data=self.post_template % (feature.id, True), content_type='application/json') # Then identityFeature = identity.identity_features self.assertEquals(response.status_code, status.HTTP_201_CREATED) self.assertEquals(identityFeature.count(), 1) Helper.clean_up()
def handle(self, *args, **options): with open(FILE_NAME, 'r') as features: features.next() # skip header reader = csv.reader(features, delimiter='\t') for row in reader: try: teams = self.get_team_objects( filter(None, row[TEAMS].split(';'))) if row[FEATURE]: feature = Feature(name=row[FEATURE], theme=row[THEME], clarity_or_jira_id=row[ID], url=row[URL]) feature.save() feature.teams.add(*teams) feature.save() except Exception as e: print('Error importing {} - {}'.format(row, e))
def test_adjust_cart(self): """ This tests if when a feature is in a cart than when the adjust view method is run, it deletes the so called feature from the cart """ admin = User(username="******") admin.save() feature = Feature(name="test", description="test", status="done", upvotes=0, views=0, author=admin) feature.save() session = self.client.session page1 = self.client.get("/cart/add{0}".format(feature.id)) page2 = self.client.get("/cart/adjust{0}".format(feature.id)) session = self.client.session cart = session['cart'] self.assertEqual(cart, {})
def test_add_to_cart(self): client = Client() self.user = User.objects.create_user('john10', '*****@*****.**', 'john10') self.user.save() self.client.login(username="******", password="******") #create feature item = Feature(name="Create a Test featture", description="test description", price=30.00, author=self.user) item.save() #add detials to cart response = self.client.post("/cart/add/{0}".format(item.id), { 'quantity': 2, 'author': self.user }) #check after post, that redirect was call '302' self.assertEqual(response.status_code, 302) #redirect goes to /features/ self.assertEqual(response.url, '/features/')
def create_test_feature(self): feature = Feature(name='Test', description='Test description', author=self.user) feature.save() return feature
class TestCartViews(TestCase): def setUp(self): self.user = User.objects.create_user(username='******', password='******') self.feature = Feature(title="Test Feature", author=self.user, content="Test Content") self.feature.save() def test_empty_cart_view(self): self.client.login(username='******', password='******') page = self.client.get('/cart/') self.assertEqual(page.status_code, 200) self.assertTemplateUsed(page, 'cart/cart.html') def test_contribution_amount(self): self.client.login(username='******', password='******') page = self.client.get('/cart/contribute/{0}'.format(self.feature.id)) self.assertEqual(page.status_code, 200) self.assertTemplateUsed(page, 'cart/contribution_amount.html') def test_add_to_cart(self): self.client.login(username='******', password='******') page = self.client.post('/cart/add/{0}'.format(self.feature.id), {'contribution': '50'}, follow=True) self.assertEqual(page.status_code, 200) self.assertTemplateUsed(page, 'cart/cart.html') def test_adjust_cart_minimum_amount_10_euro(self): self.client.login(username='******', password='******') page = self.client.post('/cart/adjust/{0}'.format(self.feature.id), {'contribution': '10'}, follow=True) self.assertEqual(page.status_code, 200) self.assertTemplateUsed(page, 'cart/cart.html') def test_adjust_cart_below_minimum_amount_10_euro(self): self.client.login(username='******', password='******') page = self.client.post('/cart/adjust/{0}'.format(self.feature.id), {'contribution': '9'}, follow=True) messages = list(page.context['messages']) self.assertEqual(page.status_code, 200) self.assertTemplateUsed(page, 'cart/cart.html') self.assertEqual(len(messages), 1) self.assertEqual(str(messages[0]), 'Contributions must be at least €10') def test_adjust_cart_0_euro_or_empty(self): self.client.login(username='******', password='******') page = self.client.post('/cart/adjust/{0}'.format(self.feature.id), {'contribution': '0'}, follow=True) messages = list(page.context['messages']) self.assertEqual(page.status_code, 200) self.assertTemplateUsed(page, 'cart/cart.html') def test_empty_cart(self): self.client.login(username='******', password='******') page = self.client.post('/cart/add/{0}'.format(self.feature.id), {'contribution': '50'}, follow=True) page = self.client.post('/cart/empty-cart', follow=True) messages = list(page.context['messages']) self.assertEqual(page.status_code, 200) self.assertTemplateUsed(page, 'cart/cart.html') self.assertEqual(len(messages), 1) self.assertEqual(str(messages[0]), 'You have successfully emptied your cart!') def test_remove_feature_from_cart(self): self.client.login(username='******', password='******') page = self.client.post('/cart/add/{0}'.format(self.feature.id), {'contribution': '50'}, follow=True) page = self.client.post('/cart/remove/{0}'.format(self.feature.id), follow=True) messages = list(page.context['messages']) self.assertEqual(page.status_code, 200) self.assertTemplateUsed(page, 'cart/cart.html') self.assertEqual(len(messages), 1) self.assertEqual(str(messages[0]), 'Feature successfully deleted from cart!')