Skip to content

Commit

Permalink
Task complete: Fix tests (#15)
Browse files Browse the repository at this point in the history
* Add procfile for heroku configuration

* updated travis.yml file

* Add Procfile edited command

* [Chore] Correct procfile name

* [Chore] Remove log file argument

* [Chore] Change site root

* [Chore] Change to gunicorn

* [Chore] Add to allowed hosts

* postgres version modified

* [chore] add Django Heroku configuration code

* Ch configure circleci coveralls 143497249 (#1)

* Added Travis configurations

* Added coverage configurations

* Added coverage configurations

* Configure deployment to heroku (#7)

* [chore] Configure db to use env variable

* [chore] Add allowed hosts

* [chore]allow wider allowed hosts option

* [chore]Configure static files

* #143489547 Api tests (#14)

* [chore] Configure db to use env variable

* [chore] Add allowed hosts

* [chore]allow wider allowed hosts option

* [chore]Configure static files

* Initial tasks complete (#6)

* Add procfile for heroku configuration

* updated travis.yml file

* Add Procfile edited command

* [Chore] Correct procfile name

* [Chore] Remove log file argument

* [Chore] Change site root

* [Chore] Change to gunicorn

* [Chore] Add to allowed hosts

* postgres version modified

* [chore] add Django Heroku configuration code

* Ch configure circleci coveralls 143497249 (#1)

* Added Travis configurations

* Added coverage configurations

* Added coverage configurations

* Configure deployment to heroku (#7)

* [chore] Configure db to use env variable

* [chore] Add allowed hosts

* [chore]allow wider allowed hosts option

* [chore]Configure static files

* [Chore]Fixed the API tests admin,badge and check model

* [Chore] Add test channel assignment. Add new tests

* the api tests pass

* updated the gitignore file

* [Chore] Remove IPDB code. Adjust to PEP 8 length

* [Chore] used .format instead of string formatting

* [Chore] returned sig instead of svg

* [Chore]Replace hard code

* changed REGISTRATION_OPEN to True in settings.py

* removed comments from create_check

* [Chore] Insert correct assertions

* #143401483 Add Account Tests  (#12)

* [account-test] test_login,test_switch_team,test_profile

* [account-test] test_check_token,test_profile

* [account-test] test_check_token,test_profile(corrections)

* [account-test] test_team_access_ware

* [account-test] test_login

* [account-test] test_team_acces_middleware,test_login(corrections)

* PR review rectifications

* [Feature #143401483]Correct assertions

* [Feature #143401483]Correct assertions]

* [Feature #143401483]Correct assertions]

* #143401485 Add front end tests. (#11)

* Added engine congigurations and url for database

* Added tests for multiple access

* [front-test]Added tests for validating pushover priority.

* [front-tests] Modified tests for validating pushover priority

* [front-tests] Modified tests for test_add_check and test_add_pushover

* [front-tests] Added tests for the icon color change green, amber and red.

* [front-test] Added test for validating bad token

* [Feature #143401485] Correct assert statements

* [front-test] modified registration in settings and removal of ipdb from the code.
  • Loading branch information
Eric G committed May 9, 2017
1 parent 9f8c37d commit a5263c2
Show file tree
Hide file tree
Showing 24 changed files with 258 additions and 137 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -6,3 +6,4 @@ hc/local_settings.py
static-collected

.DS_Store
.cache
9 changes: 6 additions & 3 deletions hc/accounts/tests/test_check_token.py
Expand Up @@ -14,13 +14,16 @@ def test_it_shows_form(self):
self.assertContains(r, "You are about to log in")

def test_it_redirects(self):
r = self.client.post("/accounts/check_token/alice/secret-token/")
self.assertRedirects(r, "/checks/")
response = self.client.post("/accounts/check_token/alice/secret-token/")
self.assertRedirects(response, "/checks/")

# After login, token should be blank
self.profile.refresh_from_db()
self.assertEqual(self.profile.token, "")

### Login and test it redirects already logged in
self.assertEqual(response.status_code, 302)

### Login with a bad token and check that it redirects
### Login with a bad token and check that it redirects
c = self.client.post("/accounts/check_token/rose/secret-token/")
self.assertEqual(c.status_code, 302)
19 changes: 14 additions & 5 deletions hc/accounts/tests/test_login.py
Expand Up @@ -11,26 +11,34 @@ class LoginTestCase(TestCase):
def test_it_sends_link(self):
check = Check()
check.save()
count_before = User.objects.count()

session = self.client.session
session["welcome_code"] = str(check.code)
session.save()

form = {"email": "alice@example.org"}

r = self.client.post("/accounts/login/", form)
assert r.status_code == 302
form = {"email": "joan.awinja@andela.com"}
resp = self.client.post("/accounts/login/", form)
self.assertRedirects(resp, "/accounts/login_link_sent/")


### Assert that a user was created

user = User.objects.get(email=form["email"])
count_after = User.objects.count()
self.assertEqual(count_after,count_before + 1)


# And email sent
self.assertEqual(len(mail.outbox), 1)
subject = "Log in to %s" % settings.SITE_NAME

### Assert contents of the email body
self.assertIn('To log into {}'.format(settings.SITE_NAME), mail.outbox[0].body)

### And check should be associated with the new user
self.assertEqual(check.get_status("alice@example.org"), "new")


def test_it_pops_bad_link_from_session(self):
self.client.session["bad_link"] = True
Expand All @@ -48,7 +56,8 @@ def test_it_handles_missing_welcome_check(self):
form = {"email": "alice@example.org"}

r = self.client.post("/accounts/login/", form)
assert r.status_code == 302
self.assertRedirects(r, "/accounts/login_link_sent/")


# An user should have been created
self.assertEqual(User.objects.count(), 1)
Expand Down
36 changes: 24 additions & 12 deletions hc/accounts/tests/test_profile.py
Expand Up @@ -12,39 +12,50 @@ def test_it_sends_set_password_link(self):
self.client.login(username="alice@example.org", password="password")

form = {"set_password": "1"}
r = self.client.post("/accounts/profile/", form)
assert r.status_code == 302
response = self.client.post("/accounts/profile/", form)
assert response.status_code == 302

# profile.token should be set now
self.alice.profile.refresh_from_db()

token = self.alice.profile.token

self.assertTrue(len(token) > 10)

### Assert that the token is set
# Assert that the token is set
self.assertEqual(response.status_code, 302)

### Assert that the email was sent and check email content
# Assert that the email was sent and check email content
self.assertEqual(len(mail.outbox), 1)
self.assertIn("Here's a link to set a password for your account on {}:".format(settings.SITE_NAME), mail.outbox[0].body)

def test_it_creates_api_key(self):
self.client.login(username="alice@example.org", password="password")

form = {"create_api_key": "1"}
r = self.client.post("/accounts/profile/", form)
### Test it creates API key
response = self.client.post("/accounts/profile/", form)

# Test it creates API key
self.assertContains(response, 'The API key has been created!')

def test_it_revokes_api_key(self):
self.client.login(username="alice@example.org", password="password")

form = {"revoke_api_key": "1"}
r = self.client.post("/accounts/profile/", form)
### Test it revokes API key

response = self.client.post("/accounts/profile/", form)
# Test it revokes API key
self.assertContains(response, "The API key has been revoked!")

def test_it_sends_report(self):
check = Check(name="Test Check", user=self.alice)
check.save()

self.alice.profile.send_report()

###Assert that the email was sent and check email content
# Assert that the email was sent and check email content
self.assertEqual(len(mail.outbox), 1)
self.assertIn("This is a monthly report sent by {}.".format(settings.SITE_NAME), mail.outbox[0].body)

def test_it_adds_team_member(self):
self.client.login(username="alice@example.org", password="password")
Expand All @@ -57,11 +68,12 @@ def test_it_adds_team_member(self):
for member in self.alice.profile.member_set.all():
member_emails.add(member.user.email)

### Assert the existence of the member emails

# Assert the existence of the member emails
self.assertTrue("frank@example.org" in member_emails)

###Assert that the email was sent and check email content
# Assert that the email was sent and check email content
self.assertEqual(len(mail.outbox), 1)
self.assertIn("You will be able to manage their existing monitoring checks and set up new\nones.", mail.outbox[0].body)

def test_add_team_member_checks_team_access_allowed_flag(self):
self.client.login(username="charlie@example.org", password="password")
Expand Down
13 changes: 8 additions & 5 deletions hc/accounts/tests/test_switch_team.py
Expand Up @@ -11,31 +11,34 @@ def test_it_switches(self):
self.client.login(username="bob@example.org", password="password")

url = "/accounts/switch_team/%s/" % self.alice.username
r = self.client.get(url, follow=True)
response = self.client.get(url, follow=True)

### Assert the contents of r
self.assertEqual(response.status_code, 200)

def test_it_checks_team_membership(self):
self.client.login(username="charlie@example.org", password="password")

url = "/accounts/switch_team/%s/" % self.alice.username
r = self.client.get(url)
response = self.client.get(url)
### Assert the expected error code
self.assertEqual(response.status_code, 403)

def test_it_switches_to_own_team(self):
self.client.login(username="alice@example.org", password="password")

url = "/accounts/switch_team/%s/" % self.alice.username
r = self.client.get(url, follow=True)
response = self.client.get(url, follow=True)

### Assert the expected response code
self.assertEqual(response.status_code, 200)

def test_it_handles_invalid_username(self):
self.client.login(username="bob@example.org", password="password")

url = "/accounts/switch_team/dave/"
r = self.client.get(url)
self.assertEqual(r.status_code, 403)
response = self.client.get(url)
self.assertEqual(response.status_code, 403)

def test_it_requires_login(self):
url = "/accounts/switch_team/%s/" % self.alice.username
Expand Down
7 changes: 5 additions & 2 deletions hc/accounts/tests/test_team_access_middleware.py
Expand Up @@ -6,12 +6,15 @@
class TeamAccessMiddlewareTestCase(TestCase):

def test_it_handles_missing_profile(self):
count_before = Profile.objects.count()
user = User(username="ned", email="ned@example.org")
user.set_password("password")
user.save()

self.client.login(username="ned@example.org", password="password")
r = self.client.get("/about/")
self.assertEqual(r.status_code, 200)
response = self.client.get("/about/")
self.assertEqual(response.status_code, 200)

### Assert the new Profile objects count
count_after = Profile.objects.count()
self.assertEqual(count_after,count_before + 1)
1 change: 0 additions & 1 deletion hc/api/decorators.py
Expand Up @@ -33,7 +33,6 @@ def wrapper(request, *args, **kwds):
request.json = json.loads(request.body.decode("utf-8"))
except ValueError:
return make_error("could not parse request body")

if "HTTP_X_API_KEY" in request.META:
api_key = request.META["HTTP_X_API_KEY"]
else:
Expand Down
12 changes: 6 additions & 6 deletions hc/api/tests/test_admin.py
Expand Up @@ -7,16 +7,16 @@ class ApiAdminTestCase(BaseTestCase):
def setUp(self):
super(ApiAdminTestCase, self).setUp()
self.check = Check.objects.create(user=self.alice, tags="foo bar")

### Set Alice to be staff and superuser and save her :)
self.alice.is_staff = True
self.alice.is_superuser = True
self.alice.save()

def test_it_shows_channel_list_with_pushbullet(self):
self.client.login(username="alice@example.org", password="password")

Channel.objects.create(user=self.alice, kind="pushbullet",
value="test-token")

### Assert for the push bullet
chan = Channel.objects.create(user=self.alice, kind="pushbullet",
value="test-token")
self.assertEqual(chan.kind, "pushbullet")

def test_it_shows_channel_list_with_unverified_email(self):
self.client.login(username="alice@example.org", password="password")
Expand Down
11 changes: 6 additions & 5 deletions hc/api/tests/test_badge.py
Expand Up @@ -12,13 +12,14 @@ def setUp(self):
self.check = Check.objects.create(user=self.alice, tags="foo bar")

def test_it_rejects_bad_signature(self):
r = self.client.get("/badge/%s/12345678/foo.svg" % self.alice.username)
### Assert the expected response status code
resp = self.client.get("/badge/{}/12345678/foo.svg" .format(self.alice.username))

self.assertEqual(resp.status_code, 404)

def test_it_returns_svg(self):
sig = base64_hmac(str(self.alice.username), "foo", settings.SECRET_KEY)
sig = sig[:8].decode("utf-8")
url = "/badge/%s/%s/foo.svg" % (self.alice.username, sig)
url = "/badge/{}/{}/foo.svg".format(self.alice.username, sig)
resp = self.client.get(url)

r = self.client.get(url)
### Assert that the svg is returned
self.assertContains(resp, "svg", status_code=200)
10 changes: 8 additions & 2 deletions hc/api/tests/test_check_model.py
Expand Up @@ -13,11 +13,17 @@ def test_it_strips_tags(self):
check.tags = " foo bar "
self.assertEquals(check.tags_list(), ["foo", "bar"])

### Repeat above test for when check is an empty string
check.tags = ""
self.assertEquals(check.tags_list(), [])

# Repeat above test for when check is an empty string

def test_in_grace_period_handles_new_check(self):
check = Check()
### Test that when a new check is created, it is not in the grace period

check.status = "new"
self.assertFalse(check.in_grace_period())
# Test that when a new check is created, it is not in the grace period

def test_status_works_with_grace_period(self):
check = Check()
Expand Down

0 comments on commit a5263c2

Please sign in to comment.