Skip to content

Instantly share code, notes, and snippets.

@NicholusMuwonge
Created April 5, 2019 14:00
Show Gist options
  • Save NicholusMuwonge/1be4a3e241a5dabedb9edcfc5f56d33c to your computer and use it in GitHub Desktop.
Save NicholusMuwonge/1be4a3e241a5dabedb9edcfc5f56d33c to your computer and use it in GitHub Desktop.
class UserRetrieveUpdateAPIViewTestCase(TestCase):
"""
This class defines the test suite for the view
that retrieves and updates a user
"""
def setUp(self):
self.user_data_2 = {
"username": "nicksbro",
"email": "nicholus@gmail.com",
"password": "Enter1234"
}
self.user_data = {
"username": "janejones2",
"email": "nicholusmuwonge@gmail.com",
"password": "Enter1234"
}
self.activate_url = reverse('authentication:activate-account')
self.register_url = reverse('authentication:register')
self.login_url = reverse('authentication:login')
self.user_action_url = reverse('authentication:user-action')
self.client = APIClient()
response = self.client.post(
self.register_url, {"user": self.user_data_2}, format="json"
)
response = self.client.post(
self.register_url, {"user": self.user_data}, format="json")
# print("Response ", response.data)
self.token = response.data["data"]["token"]
self.client.get(f"{self.activate_url}?token={self.token}", format='json')
response = self.client.post(
self.login_url, {"user": self.user_data}, format='json')
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
def test_verify_account_with_invalid_token(self):
invalid_token = "icVpZnJlZCIsImV4cCI6MTU1ODY3"
self.client.get(f"{self.activate_url}?token={invalid_token}", format='json')
self.client.credentials(HTTP_AUTHORIZATION='Token ' + invalid_token)
response = self.client.get(self.user_action_url, format="json")
self.assertIn(
"Invalid authentication. Could not decode token.",
response.data["detail"])
def test_invalid_verification_link(self):
invalid_token = "icVpZnJlZCIsImV4cCI6MTU1ODY3"
response = self.client.get(f"{self.activate_url}?token={invalid_token}", format='json')
self.assertIn(
"Verifcation link is invalid. Check email for correct link.",
response.data["detail"])
def test_api_can_retrieve_a_registered_user(self):
response = self.client.get(self.user_action_url, format="json")
self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_api_needs_authentication_to_retrieve_a_user(self):
self.client.logout()
response = self.client.get(self.user_action_url, format="json")
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_api_can_update_user_data(self):
new_user_data = {
"email": "jjones@email.com",
"bio": "I like eggs for breakfast",
"image": "https://myimages.com/erwt.png"}
response = self.client.put(
self.user_action_url, {"user": new_user_data}, format="json")
self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_api_needs_authentication_to_update_user_data(self):
new_user_data = {
"email": "jjones@email.com",
"bio": "I like eggs for breakfast",
"image": "https://myimages.com/erwt.png"}
self.client.logout()
response = self.client.put(
self.user_action_url, {"user": new_user_data}, format="json")
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_api_get_user_data_with_bad_authorization_header(self):
response = self.client.post(
self.login_url, {
"user": {"email": "jjones@email.com", "password": "Enter-123"}
}, format='json')
self.client.credentials(HTTP_AUTHORIZATION='Fred ' + self.token)
response = self.client.get(self.login_url, format="json")
self.assertIn("Bad Authorization header.", response.data["detail"])
def test_api_get_user_data_with_invalid_token(self):
invalid_token = "icVpZnJlZCIsImV4cCI6MTU1ODY3"
self.client.credentials(HTTP_AUTHORIZATION='Token ' + invalid_token)
response = self.client.get(self.user_action_url, format="json")
self.assertIn(
"Invalid authentication. Could not decode token.",
response.data["detail"])
def test_api_can_allow_user_to_view_another_person_status(self):
response = self.client.get(
'/api/profiles/janejones2',
format="json")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertIn("janejones", response.data["username"])
self.assertIn("https://static.productionready.io/images/smiley-cyrus.jpg", response.data["image"])
def test_profile_created_on_user_registration(self):
response = self.client.get(
'/api/profiles/nicksbro',
format="json")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertIn("", response.data["username"])
self.assertIn("https://static.productionready.io/images/smiley-cyrus.jpg", response.data["image"])
def test_api_cannot_retrieve_a_non_existing_profile(self):
response = self.client.get(
'/api/profiles/jane',
format="json")
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
self.assertIn(
"The requested profile does not exist.",
response.data["errors"]["detail"])
def test_api_user_cannot_other_persons_profiles(self):
new_user_data = {
"email": "jjones@email.com",
"bio": "I like eggs for breakfast",
"image": "https://myimages.com/erwt.png"}
response = self.client.put(
'/api/profiles/nicksbro',
{"user": new_user_data},
format="json")
self.assertEqual(
response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED
)
def test_user_can_view_their_own_status(self):
response = self.client.put(
'/api/user/',
format="json")
self.assertEqual(response.status_code, status.HTTP_200_OK)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment