Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for add_student view and moved tests in students app to tests folder #262

Closed
wants to merge 1 commit into from
Closed

Add tests for add_student view and moved tests in students app to tests folder #262

wants to merge 1 commit into from

Conversation

harshakhmk
Copy link
Contributor

Related Issue

Fixes: 79

Prosposed Changes

  • Add test for GET and POST methods of authenticated and unauthenticated users.
  • Check redirecting URLs and response status code for unauthenticated users

Additional Info

  • Due to adding the tests folder in apps/students folder, we need to delete existing tests.py to run the test scripts
    tests_feature

Screenshots

  • My tests got passed with no errors
Original Updated
** original screenshot ** ** updated screenshot **

@Jagrati-Bot
Copy link
Collaborator

Congratulations for making your first Pull Request at JagratiWebApp!! 🎉 Someone from our team will review it soon.

Copy link
Owner

@garg3133 garg3133 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@harshakhmk I don't understand what you're actually testing here? You've created 2 users but you're running both tests for the same user (logged in one). You've written in the comment that the above test is for the unauthenticated user but that test is also running for the [email protected] (it is being redirected because this user also doesn't have an authenticated profile, which you need to create).

Here are a few more pointers:

  • Use specific class name based on the view you are testing (like AddStudentViewTest for this particular class.
  • You need to create separate functions for each case you are testing, and not just test everything at a single place.
  • You need to test the flow of the complete view in different scenarios, like without login, with login and no profile, with login and complete profile, test if the user is being redirected correctly in each case and if user is logged in with authenticated profile, test if correct template is being displayed, etc.

You can refer to this comment on the issue for more details on how the tests are to be written.

Also, you may refer to this tests file to get an idea of how tests are actually written: https://github.com/garg3133/fossee/blob/master/roomslotbooking/home/tests/test_views.py (Also notice the name of the class and different functions there).

At last, you should give this tutorial a read: https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Testing#views
Everything is mentioned in great details here on how tests are actually written in Django.

Feel free to reply back here if you're stuck somewhere or don't understand anything.

@harshakhmk
Copy link
Contributor Author

harshakhmk commented May 15, 2021

Ok, I will update my PR within 2 days
Does create a superuser for testing is fine so that he can pass all required permissions?

@garg3133
Copy link
Owner

Does create a superuser for testing is fine so that he can pass all required permissions?

No, you should replicate the real situation in the tests, how actual users might behave while using the application.

@harshakhmk
Copy link
Contributor Author

harshakhmk commented May 22, 2021

I had a doubt, even though when I log in and try to make a request to add_student URL then the redirected URL becomes
/accounts/login/?next=/students/add/ Since I was already logged in whats the use of redirecting it to accounts/login
and also it showing same for complete profile user and incomplete profile user

@garg3133
Copy link
Owner

@harshakhmk You must have forgotten to set auth=True in User model. When this happens, user is redirected to complete profile page initially, and the profile being complete but not authenticated by admin, user is logged out, and redirected to login page. (A user cannot access any page unless the profile is complete and authenticated by admin.

@harshakhmk
Copy link
Contributor Author

harshakhmk commented May 23, 2021

How do I handle authenticating the user by admin in testing, also self.client.login() function is returning false after passing correct credentials
I tried setting auth=True

login = self.client.login(email='[email protected]', password='password1')
        user=User.objects.filter(email='[email protected]')
        user.auth=True
        print(login)
        response = self.client.get(reverse('students:add_student'))
        
        print(response)
        self.assertRedirects(response, "/accounts/login/?next=/students/add/", status_code=302, target_status_code=200, msg_prefix='', fetch_redirect_response=True)

but response doesn't get changed, also there is no approved field in user for admin to approve an account

@garg3133
Copy link
Owner

garg3133 commented May 23, 2021

@harshakhmk For successfully accessing the add_student page, the user must have a User model entry, with active=True and auth=True and must have an entry in Profile model. There was one other permission required to access the page, which I have removed (you can pull the latest changes for that to take effect in your code).

auth=True is how admin verify a profile. But you need to do that before trying the client to log in, and also save the changes using user.save().

@garg3133 garg3133 closed this May 30, 2021
@harshakhmk harshakhmk deleted the test-new-student-view branch May 30, 2021 15:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unit tests for new_student view
3 participants