API Testing Tutorial. Unit Testing Authentication. Django rest framework project tutorial[14]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
our guys will come back so up to this point we built out a lot of end points for up after we are coming close to concluding it but I wanna start talking about API testing so no it's it's usually thought of as an afterthought and most people don't you do it but in certain environments rocks and when you're in on a team it's always going to be easy for someone to break existing code because everyone will be working on different parts of the code and they would want to modify something somewhere else in the code and that kind is easy to like break the whole without knowing so when you add this to your code it can prevent this this errors from happening at an earlier stage still the same way so they can act as the documentation for the code so if we touch something to say don't touch this this code should do this or if you doesn't do that then the test will fail and everyone will be seeing that you're breaking something and they will be yelling at you so in this one we are going to add some tests to make sure that the user can sign up to application to make sure they can log in and also make sure that they can't log in if they have not verified the email so it's gonna be awesome we should start but before we do that I highly recommend you subscribe to the channel as we are going to continue learning a lot of good stuff like this but now this that alright so I'm here in my text editor so one thing you will notice that whenever you create an app it will always come with this disability why fire this is why you can write your test cases but for the most part when you have an app like this that has many fires so we have we tools we have views we have stereo rises and you want to test all of these using one file you will not be efficient enough so it's always good that you create another folder in the app and then you put all the tests for that up there so right now I'm going to delete this one and then in the app I'm going to create a new file a new folder called tests and then I'm gonna have I'm gonna have a fire code to enter in it's good py alright so and of course I should move this to the app should be in the app or a sureno you can see that we have we have created a new folder and it has it done in it's fire and if you're not familiar with it standard in it fail basically it makes - look at this folder as a mode you say that we can import it everything we put in here in other files so in here now we can write our tests in here but I want to show you what happens when we have noticed so now if we go to our terminal and run Python when I just be rightist what will happen is it's going to try to find it isn't it finds nothing and then you can see that it render a test and then okay so this add some tests so right here let's say we want to be able to test our views this dossier rises or models and every file that we want and and every file that we have created so you're going to need to create separate folders what it is so if you want to manage it better but for now I'm going to create different files to tests different things so one is gonna be this use py let's say we want to test the model we can create another one like lists underscore model model dot P Y and then Raina when you're working with this we are going to be having a lot of things that we need to do that as Samira for example if you think of it before we run a test you might want to have like testing data to use for every test that we run every test that we run so it's always going to be important to create like a setup file that can do the things for us so here I'm going to create another fire I'm going to call it test satyr go to py so in here I'm gonna play the class called test setup and then in here we can now we can now create our setup methods but before we do that when it comes to the jungle risk framework they give us a couple of utilities that you can use to test our applications and one of them is bring it in from rest framework good test input API actually API test case all right so with this we can now get access to all the juicy stuff that we vanish the test our endpoints and all that good stuff so we have to inherit from it and now in here I want to setup copy a couple of thing we are going to be reusing so one of them will be the decorations for our our URLs and then and then some testing data so I'm going to import a TTT pod reverse jungle URLs input robust okay so what rebus does the text in a view name and then it gives us a path to the route so you can think of it as a URL that we can make a request if we want to get the URL for the registration endpoint you can do something like servlet register your room this is gonna be equal to a plus and then it was takes in the view name so if we take a look at our URL stood py which is here you'll notice that our register road is called register so I'll bring that funny so this should take care of doing that for us to then also get the login so let me get a look in we should be looking I believe let's cross-check yes it is okay so once we have this we basically need to have them in a method called set up so I'm gonna cut them out and then I'm gonna create a method called set up I'm gonna bring in another one called tear down these two methods here they are called for every test that run so in here yeah so now we can define all our variable that we want to be accessible in every test method that we run so whenever I test runs it's gonna be running this so it's gonna run this and then when it's done turns this and then it goes another test it runs this then it runs the tests of course and it runs this all right so those are the two that we have set out but then when we are going to like create a user we are going to need to take user data to test week so I'm gonna create a variable called user data so this is going to be equal to a dictionary since as you may know we need to think like the email so I can have like email Gmail and then we are going to need the username and then the password alright so I'm gonna use the email as my username because you know we have validations even catch that she didn't have username data and what's alphanumeric so this is what you're gonna set up so I'm gonna put the views and then I'm going to create a class to test maybe it's test our views so class gonna call it test use this is going to inherit from the class that we've already made which is here in test setup and this College just set up what is it a generic name so I'm going to input it here so from the test setup import the class so inherit from it just so we can get access to everything that is defined here so once we get those now I'm going to create a function to test that the user can register so let's say tests user can register all right so it since this the class tips himself and then in here now we can saturate in our session so what the sessions are basically you can think of them as a comparison take checks okay so here let's say we want to make a request and see if we can get response from the API so here when we import from from API test case we get access to something called an API client which is this one so once we inherit from here we get access from it and now that we are inheriting from that class which means that we already have it here so here you can do self dot client dot post as you know we are sending a post request the registration and then here we need to send a path so you can say self dot register URL all right so this as you may think of it would probably not work cause we are not sending the the data we are sending the user data but what I want us to do is actually check that the user can't sign up by just clicking a button without submitting anything so I'm going to change the test Nate the test special method so I'm gonna call Eastern not register we know better when we make a request it gives us a response so we can save the response and now we can run our sessions of this response so what do we expect thing is in this case we basically expect that we are going to get an error maybe a four-four or something so we can write this dude status clerk so we expect this one to equal to 400 I believe so I'm going to now run an assertion to basically check for this so when we inheriting from these classes we can already run a set equal as such equal and then we give it two things so one what you are going to be checking and what would think what what would would think it would be will be equal to so we actually expect that raise that status code will be 400 so I'm going to save that and then I'm going to go to my terminal and then we run these tests so when I run the tests you can see that now we have one run and then we can see a check meaning that it passed so let's say we expect a rate 200 here with the expect is to fail let's see you can set it first and we basically are getting an error instead so I want us I want to show you guys how you can inspect this so I'm gonna add a PDB here which if you don't know is take a bite on debugger enables you to post a program execution and look at the variables so let's take a look at this value so if you run the test again we can see that it's pauses and then if we look at risk so you see it's a four it's a four hundred response so meaning that we say that's what you want but if you want see like which data is being sent down you can run race to data and now you can see everything here so you can run all your sessions on these you see before the data that we have so you can also run a search engine bills but I'm going to keep it simple here and then this smooth one and I show you most of so I'm gonna actually cut this one out so if you want to test that a user can sign up correctly we're gonna duplicate this one so let's say a user can sign up user can register correctly and there is feel free to add more assertions here so you can actually add more you can check the data check the response codes under that stuff so here we want to check if visit and signups successfully so first thing we pass the URL and now we need to pass the data so you can pass self load data how do you call it let's take a look at the setup okay so we need to add self via just so we can access it when we hear it from this class so it's called user data alright so the second parameter we can pass the data so safe go to user data so save that and now that we have seen in JSON we can define the format so that's the format it was Jason alright so what we would expect now actually me change this to 400 again now we would expect to have a 201 created because we know everything is valid you can have something like rests equals this and then we can assert for it 201 all right so if we run this run the test again you can set the whole pass means that the Iza was created in fact if we look at the race if we do a PDB and then run again and do risk data you see we get our user created and being sent that week so you can actually check this so for example right here you can add another session like I was saying can do something like served assert equals ax it's a set that stress good email actually should be raised team a will be equal to the email that we have so the email that we sent you can we can also get the imager to Center and get it from user and then it's email so once we try that the server can send us back the email once we create an account and yeah we can do the same thing for the username so you can do the same thing for the username and you can run any Python comparison here so if we run this one again we run it also you can see that this now is failing so if we do let's see so this should be raised to data okay whispered data okay so what you're saying is in the data response that we get the key email should be equal to the email that we sent to the server so if we run run this you can see that they all pass all right so let's test for for a user to authenticate correctly okay so first thing just first write a test to make sure the user who's not verified cannot log n so test so their test and guys the one writing these test methods you can see I prefix everything with test underscore and this is very important you also need to do it like this as this is what jungle uses to find these tests so this should be test user cannot login we and and verified email okay that's in so just a long name of the function as you guys can make it smaller all right so right here you notice that when you create a user here you basically get to user but that you say if you come to think of it that user is not verified so we don't expect them to be able to log in so I'm going to come bring this one here so here we are making it requested the registration so we are registering and we are sure we are doing that cuz just take it up there and once we do that now we can make another request to to login so to make a list to login we use the client of course and then we post so we want to post to broken that it should be served a plug-in URL and then the next one will be the data to want to send so here we can actually send the same data because it's the same payload and the endpoint will use the password and they and they email from there so you can keep this so it saved the response from this in the in a variable race so now we would expect is to be 401 so if you think of it we'll think of endpoints you should tell us if you want you can assert yourself put assets equal so we want race blood status code and this should be a photo so depending on how the API structure it can be different anyone how you if you want you intend to implement it so if you run this you can see that it passes and you might ask you might be asking what is happening here cause we've created a user yes but the MN able to login so the reason for that is because the account is not verified actually if we take a look here we take a look at the race let me add a PDB and then we run this test no not a Sava won't run the test so if we look at risk to theta because that we get email is not verified all right so so how do we go ahead and read and login and test for this case so what you can do is you can actually manually verify this user through code and then test if they can log in after verification so you can actually keep this and then create another test case then here we can test user can log in test user can login after verification alright so right here what you're gonna do when we make a replace to sign up a user we are going to save that in RIS can quiet even response just once so when you make a request to sign up I use and gives us a response it means that we are going to be getting the new data that will be sent back so now you can have phase as email yeah and this is going to be able to response but data then email and once we get that now we can manually activate this user using the user mode oh so do we do that let's bring in the user mode and here you can really important thing you want from from Python so I'll show you can you put it from mother's input user all right so let's verify the user so here we can do user because user objects gate so what to get by email so add email it should be equal to now the email I took out and now this verify them here so you can the user it's verified verified equals true and then let's save so user let's save so now we will go ahead and verify the user manually so when we make a request to the endpoint again we are going to expect this to be this user to be able to log in so we are going to expect a 200 all right so let's test that we come over here and rerun the test you can set the ole pass good so yeah that's gonna do it for this one but maybe one last thing I can show you is that you noticed how we change the setup we quite like hard-coded the testing data so record you guys look into something called fixtures but I want to show you something if you want to get like the dynamic data here and you don't want to have good via you can use a package called fake huh so here I'm gonna bring it in so people store fake huh okay so I already have it so once you get that fake up emoji installed and actually used use it to generate fake data for testing okay so though use it is we imported and then we set an instance for it and then we can start using it to get the data so here when I bring it in so you can see we import it and then right here we can initialize it so instant ship they fake a class and then now we can use this paper so yeah I'm gonna excessive self good fake because this you can put it anything here J so now if you want the email we can do something like self dot fake good email and if you want a username I'm off you're going to get the username for my email so when I run got fit good email now I can sprit is and we know that our image will have an add domain so we have an add and then I will keep the first user at that domain so for the password can it'll be anything so you can even use this doesn't matter but what I want to show you is I know it doesn't really matter for the password so let's keep that I'm not you're going to use an email as a password and email format at the classmate and then I'm going to add a PDB here just so you can see how it looks like now if I come back to the app and run the tests you can set it pauses so if we take a look if we want if we take a look at user data so we do like use the data itself go to user data you can see that we have basically the data testing data using thicker so you guys feel free to check that or not if I continue so I need to put a remove the PTB because it's going to be running this so if we rerun it again you can see that whatever this is pass and now we are using dynamic data from Vega so it's going to do it for the video if you enjoyed it please give it a thumbs up consider subscribing to the channel and I'll see you in the next one
Info
Channel: Cryce Truly
Views: 32,097
Rating: undefined out of 5
Keywords:
Id: 17KdirMbmHY
Channel Id: undefined
Length: 22min 14sec (1334 seconds)
Published: Mon Jun 22 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.