1 00:00:00,150 --> 00:00:06,870 In this video we have a singular goal that is to create some more interesting test data to work with. 2 00:00:06,870 --> 00:00:13,830 Right now we create a user and set them up in the database that allows us to test things like the log 3 00:00:13,830 --> 00:00:20,160 in a functionality we want to make sure we do something similar with tasks so we can test those other 4 00:00:20,160 --> 00:00:22,110 end points as well. 5 00:00:22,170 --> 00:00:28,140 Now what does that mean for us it means we need to create some new data right inside of here and we're 6 00:00:28,140 --> 00:00:34,950 going to make some modifications to setup database to get those tasks actually created. 7 00:00:34,950 --> 00:00:40,630 The first thing I'm gonna go ahead and do is load in the task model up above. 8 00:00:40,650 --> 00:00:43,580 So right here const a task equals. 9 00:00:43,770 --> 00:00:51,430 I'll go ahead and use require loading in that model dot dot forward slash dot dot. 10 00:00:51,550 --> 00:00:54,570 Then it's as source models a task. 11 00:00:54,580 --> 00:00:59,530 And down below we'll go ahead and create a few different pieces of data. 12 00:00:59,860 --> 00:01:05,350 I'm gonna create a couple of tasks and we'll even go ahead and create a second user. 13 00:01:05,350 --> 00:01:06,850 We could play around with. 14 00:01:06,850 --> 00:01:09,350 Let's get started with that second user. 15 00:01:09,400 --> 00:01:10,090 Right here. 16 00:01:10,120 --> 00:01:16,660 I'm gonna take everything for the first user are two variables down below what I'm gonna do is just 17 00:01:16,660 --> 00:01:19,630 paste them and changing some of the information. 18 00:01:19,660 --> 00:01:25,670 So for the second one I'll have user to I.D. then the object USER 2. 19 00:01:26,200 --> 00:01:32,800 And down below where we reference the I.D. We want to make sure to grab user to I.D. here and when we 20 00:01:32,800 --> 00:01:38,600 create the JWT and then we can just update some of the data. 21 00:01:38,600 --> 00:01:45,680 So as an example I could use Andrew as the name then Andrew at example dot com and I could change the 22 00:01:45,680 --> 00:01:47,570 password as well. 23 00:01:47,570 --> 00:01:53,080 I'll use something like my house 0 9 9. 24 00:01:53,090 --> 00:01:56,490 Then I'll toss in some special characters as well. 25 00:01:56,540 --> 00:02:00,950 So now we have a second to user that we could introduce into the database. 26 00:02:00,950 --> 00:02:03,660 Down below we'll go ahead and do just that. 27 00:02:03,740 --> 00:02:05,190 We take this line. 28 00:02:05,270 --> 00:02:12,900 We duplicate it and we just change which user we're saving we go from user one over to User 2. 29 00:02:12,950 --> 00:02:19,460 So now we have two users in the database allowing us to create a more real world scenario for our test 30 00:02:19,460 --> 00:02:20,540 cases. 31 00:02:20,540 --> 00:02:24,860 Down below we can see that one of our test suites has indeed failed. 32 00:02:24,870 --> 00:02:27,050 You should sign up a new user. 33 00:02:27,050 --> 00:02:31,790 I'm actually already using that email so I'll switch this up to something else. 34 00:02:31,940 --> 00:02:39,590 I'll use Jess as the name and then just add as an example dot com as the email for that test user. 35 00:02:39,620 --> 00:02:41,990 I don't believe I've used this and I haven't. 36 00:02:41,990 --> 00:02:45,370 Down below things are working now with this in place. 37 00:02:45,380 --> 00:02:51,990 We're gonna go ahead and create some tasks for our users and we'll use those when writing our test cases. 38 00:02:52,070 --> 00:02:58,400 Right here I'll go ahead and create a new constant called the task one and we'll set this up as an object. 39 00:02:58,400 --> 00:03:04,290 Now we are going to define our own I.D. so we can make use of that in our test cases. 40 00:03:04,280 --> 00:03:09,000 There's no need to create it as a standalone variable like we did with users. 41 00:03:09,020 --> 00:03:14,870 The only reason we did that is because we needed the value in two different places down below we can 42 00:03:14,870 --> 00:03:18,290 simply create the I.D. right on the object. 43 00:03:18,290 --> 00:03:24,200 So right here that is new Mongoose dot capital T types Dot. 44 00:03:24,230 --> 00:03:26,720 Capital O object capital. 45 00:03:26,780 --> 00:03:33,920 I d now that we have that in place we can go ahead and set up the other task data we need. 46 00:03:33,920 --> 00:03:36,920 I'll go ahead and set up the description. 47 00:03:36,920 --> 00:03:41,000 So right here in the description for our task. 48 00:03:41,000 --> 00:03:43,130 So we have our task model. 49 00:03:43,130 --> 00:03:50,590 We have to provide all of this data the description for this one could be something like a first task 50 00:03:50,920 --> 00:03:57,580 and we could set completed equal to False and then we also have to go ahead and setup owner. 51 00:03:57,820 --> 00:04:04,840 So right here I'm going to set owner equal to the idea of one of my two users up above. 52 00:04:05,020 --> 00:04:05,730 Right here. 53 00:04:05,860 --> 00:04:08,350 I'll just use user 1 I.D.. 54 00:04:08,680 --> 00:04:12,190 I could also use user 1 dot underscore I.D.. 55 00:04:12,190 --> 00:04:14,060 Both are identical. 56 00:04:14,140 --> 00:04:20,890 So right here we have a task one then down below we'll end up creating a couple of other tasks. 57 00:04:20,890 --> 00:04:24,160 Now this task isn't being saved to the database just yet. 58 00:04:24,190 --> 00:04:27,250 And that's OK down below I'm going to copy. 59 00:04:27,250 --> 00:04:31,780 Task one and I'm going to convert that over to Task 2. 60 00:04:32,260 --> 00:04:38,260 I still want to generate my idea and I'll change the description to something like second task. 61 00:04:38,470 --> 00:04:44,860 And for this one I'll set it to be true in terms of its completed value and I'll leave this one for 62 00:04:44,920 --> 00:04:46,480 user 1. 63 00:04:46,480 --> 00:04:53,170 Now we'll create just one more by duplicating this object a second time down below. 64 00:04:53,170 --> 00:05:01,240 This is gonna be task 3 we'll leave the I.D. I'll change the description to third task and I can leave 65 00:05:01,240 --> 00:05:03,870 the completed value at True or false. 66 00:05:03,880 --> 00:05:09,810 Either one works and we'll set owner equal to user to dot I.D.. 67 00:05:10,000 --> 00:05:16,930 So now we have a couple of users and each have tasks giving us some real world data to use down below 68 00:05:17,290 --> 00:05:24,580 all we need to do is update setup database to actually delete the existing tasks and add these three 69 00:05:24,580 --> 00:05:25,900 into the mix. 70 00:05:25,900 --> 00:05:34,160 So down below I will use a wait with task and write here using delete many exactly like I did on the 71 00:05:34,160 --> 00:05:36,380 line above for users. 72 00:05:36,380 --> 00:05:40,940 Then down below where we create the users we will create all 3 tasks. 73 00:05:40,940 --> 00:05:42,520 I'll define a new one. 74 00:05:42,680 --> 00:05:46,060 Task 1 calling these save function. 75 00:05:46,220 --> 00:05:50,600 I can then take this line and paste it 2 times down below. 76 00:05:50,660 --> 00:05:54,550 So we save Task 1 then Task 2 woops. 77 00:05:54,560 --> 00:06:00,940 I had some extra characters in there and then after Task 2 that would be task 3. 78 00:06:01,130 --> 00:06:02,270 Excellent. 79 00:06:02,270 --> 00:06:06,970 Now with this in place there's no need to make any changes to our test suites. 80 00:06:07,040 --> 00:06:11,630 They are automatically going to use all of the new changes we've made. 81 00:06:11,630 --> 00:06:16,150 Now if we rerun things everything should be passing end it it's right here. 82 00:06:16,190 --> 00:06:22,550 We still have eleven passing test cases now that we have this more interesting data though we can take 83 00:06:22,550 --> 00:06:28,820 advantage of it over inside of our task test suite and that's exactly what we're going to do down below. 84 00:06:28,820 --> 00:06:34,670 This is going to be a challenge for you to knock out on your own right here are the challenged comments. 85 00:06:34,700 --> 00:06:40,850 You're going to write a single test case for get tasks you're going to ensure that when you fetch the 86 00:06:40,850 --> 00:06:46,030 tasks for a given user you only get those user's tasks back. 87 00:06:46,040 --> 00:06:53,330 So right here you're going to first create the test case and use request to make a request for all tasks 88 00:06:53,330 --> 00:06:55,020 for user 1. 89 00:06:55,110 --> 00:07:00,740 Then you're going to assert the correct status code and you're also going to check the length of the 90 00:07:00,740 --> 00:07:08,090 response array is 2 so over in DB dot J S How many tasks does a user want to have. 91 00:07:08,090 --> 00:07:09,370 They have to. 92 00:07:09,440 --> 00:07:13,130 That's exactly what I should be getting back if I'm getting back less. 93 00:07:13,130 --> 00:07:14,420 We have a problem. 94 00:07:14,480 --> 00:07:21,590 If I'm getting back more we also have a problem because this should not be returned as a task owned 95 00:07:21,620 --> 00:07:22,490 by user 1. 96 00:07:22,490 --> 00:07:24,950 It's owned by user 2. 97 00:07:24,950 --> 00:07:31,010 Now once you have that assertion in place you're going to test your work by running the test case and 98 00:07:31,010 --> 00:07:34,810 you should go from eleven passing tests to twelve. 99 00:07:34,880 --> 00:07:40,760 So right here take some time to knock that one out test your work and when you're done come back and 100 00:07:40,760 --> 00:07:41,420 click play 101 00:07:45,440 --> 00:07:46,220 how that go. 102 00:07:46,250 --> 00:07:54,590 Let's kick things off by calling the test function and providing a name should fetch user tasks. 103 00:07:54,590 --> 00:08:00,620 We'll set up our function as an asynchronous function and down below we'll kick things off by making 104 00:08:00,620 --> 00:08:04,850 that request so const response will store the response. 105 00:08:04,850 --> 00:08:08,900 I will await the call to request a passing in app. 106 00:08:09,230 --> 00:08:16,860 Then we'll set up dot get this is a get to request to the following end point forward slash tasks. 107 00:08:16,880 --> 00:08:22,640 Now if we were testing one of our options we could provide those here such as only getting tasks back 108 00:08:22,640 --> 00:08:29,240 that are completed or incomplete or getting them back in a specific order or a specific page. 109 00:08:29,240 --> 00:08:35,120 Right here though we just wanted everything and then down below we'll make sure to set up authentication 110 00:08:35,480 --> 00:08:44,420 so dot set setting up authorization providing the correct value that is bearer followed by the token 111 00:08:44,720 --> 00:08:50,290 user one dot tokens grabbing the first one and accessing that token property. 112 00:08:50,450 --> 00:08:53,510 Then we're going to use send to send it off. 113 00:08:53,510 --> 00:08:57,530 Now in this case there's no need to provide a request body. 114 00:08:57,530 --> 00:09:00,740 So I'm not going to provide any arguments right here. 115 00:09:00,740 --> 00:09:07,610 And finally I'll add the call to expect on to assert the status code is a two hundred. 116 00:09:07,640 --> 00:09:12,230 Now we want to make sure that the length of that response is too. 117 00:09:12,290 --> 00:09:16,030 So if you forget exactly what your data comes back as it's okay. 118 00:09:16,060 --> 00:09:22,970 If you need to look into the router file for example I could go into the task router I could find that 119 00:09:22,970 --> 00:09:27,720 specific end point and I could see what sort of structure I'm getting back right here. 120 00:09:27,830 --> 00:09:33,430 I can see I'm getting back an array that means over inside of my test is suite. 121 00:09:33,440 --> 00:09:35,540 I can set up the correct assertion. 122 00:09:35,780 --> 00:09:40,670 What I'm going to do is expect something about response dot body. 123 00:09:40,910 --> 00:09:47,860 I'm going to expect its length I'll access the length the property to equal the following number of 124 00:09:47,860 --> 00:09:48,990 the number two. 125 00:09:49,130 --> 00:09:53,140 If it's too great if it's not we have a problem. 126 00:09:53,180 --> 00:09:57,840 Now we can remove our challenge comments down below and actually test things out. 127 00:09:57,890 --> 00:10:01,850 We should have those 12 a passing test cases right here. 128 00:10:01,850 --> 00:10:04,420 That is exactly what we end up getting. 129 00:10:04,430 --> 00:10:11,090 So now we have more interesting test data allowing us to write test cases like this where we fetch data 130 00:10:11,120 --> 00:10:15,380 for a specific user and make sure that's all we get back. 131 00:10:15,380 --> 00:10:19,990 Now let's work through a second challenge of a similar nature down below. 132 00:10:20,000 --> 00:10:26,720 You're going to test the security of the delete task operation so you're going to attempt to have the 133 00:10:26,720 --> 00:10:29,750 second user delete the first task. 134 00:10:29,750 --> 00:10:33,360 Now the second user is not the owner of the first task. 135 00:10:33,440 --> 00:10:36,950 So we would expect that this operation would fail. 136 00:10:37,040 --> 00:10:41,290 Now in order to get this done you will need to export some stuff from DB. 137 00:10:41,320 --> 00:10:42,190 Dr. J asked. 138 00:10:42,200 --> 00:10:47,020 That is currently not exported all of the new stuff we created. 139 00:10:47,090 --> 00:10:53,180 Once you have that loaded into this file you're actually going to send off the request trying to delete 140 00:10:53,490 --> 00:10:57,040 that first task as the second user. 141 00:10:57,200 --> 00:11:03,440 Then you're going to assert you get the correct failure status code and you'll assert that that first 142 00:11:03,440 --> 00:11:08,120 task is still in the database it should not have been deleted. 143 00:11:08,120 --> 00:11:16,440 Finally you'll test your work by saving the test suite and looking for 13 passing test cases down below. 144 00:11:16,580 --> 00:11:18,770 So take some time to knock that one out. 145 00:11:18,770 --> 00:11:28,100 You can create the new test case right here when you're done come back and click play. 146 00:11:28,180 --> 00:11:28,810 All right. 147 00:11:28,810 --> 00:11:29,730 How'd that go. 148 00:11:29,740 --> 00:11:36,850 I'm going to kick things off by exporting the new items we created over in DB dot J S now we could export 149 00:11:36,880 --> 00:11:41,930 just what we need for example something like task one so we can get its I.D.. 150 00:11:42,100 --> 00:11:48,070 But I'll go ahead and export all three tasks as well as the data for that second user. 151 00:11:48,090 --> 00:11:49,540 That means down below. 152 00:11:49,540 --> 00:11:52,420 We're going to add some more stuff right on here. 153 00:11:52,420 --> 00:11:59,230 We'll have user to I.D. then we'll have user to we'll have a task. 154 00:11:59,230 --> 00:12:08,230 One task two and finally task three making sure the other file can access anything that it might need. 155 00:12:08,230 --> 00:12:14,250 Now we can go ahead and save this one and load in just what we actually need over here. 156 00:12:14,350 --> 00:12:20,650 So we want the second user to delete the first task which means I need both of those pieces of data 157 00:12:21,310 --> 00:12:23,230 up above when we're actually. 158 00:12:23,350 --> 00:12:28,770 Excuse me when we're dis structuring an object we typically want to keep it on one line. 159 00:12:28,780 --> 00:12:33,820 If you're just grabbing a couple of things but if you find yourself grabbing quite a few things you 160 00:12:33,820 --> 00:12:37,600 can go ahead and break that onto multiple lines like this. 161 00:12:37,750 --> 00:12:44,230 A very common thing to do and with this in place we can now easily grab other things we would like as 162 00:12:44,230 --> 00:12:45,120 well. 163 00:12:45,130 --> 00:12:53,810 For example I can grab user to I.D. I can go ahead and grab user 2 and I'll grab my tasks as well. 164 00:12:53,810 --> 00:12:56,290 Even if we're not using them right now. 165 00:12:56,390 --> 00:13:00,310 Task 1 task 2 and Task 3. 166 00:13:00,380 --> 00:13:06,230 And with this in place we're now ready to actually create that test case down below. 167 00:13:06,230 --> 00:13:11,670 Let's do just that by calling test a third time in this file right here. 168 00:13:11,670 --> 00:13:20,100 Test should not delete other users at tasks. 169 00:13:20,290 --> 00:13:21,820 You might have picked a different name. 170 00:13:21,820 --> 00:13:23,590 That's perfectly fine. 171 00:13:23,590 --> 00:13:29,560 I'll set this up as an async function and then we'll go ahead and actually make the request. 172 00:13:29,560 --> 00:13:36,550 So as that second user we want to delete the very first task I'm going to kick things off by creating 173 00:13:36,550 --> 00:13:41,820 my response variable and awaiting my call to request I'll pass in the app. 174 00:13:41,830 --> 00:13:47,110 Then we'll go ahead and set up this specific type of HDD requests we're making. 175 00:13:47,140 --> 00:13:51,040 This is using delete with the following path. 176 00:13:51,040 --> 00:13:58,750 Now the path is going to include the I.D. of that first task so I could use a regular string and use 177 00:13:58,750 --> 00:14:06,070 concatenation with the plus operator or I could use a template string either approach is perfectly fine. 178 00:14:06,070 --> 00:14:12,250 That is forward slash tasks forward slash the task I.D. right here. 179 00:14:12,250 --> 00:14:20,710 That for me will be the following that is task one dot underscore I.D. then down below we want to make 180 00:14:20,710 --> 00:14:24,740 sure we're authenticated as that second user. 181 00:14:24,880 --> 00:14:32,260 So I'll be setting up the authorization header with the value bearer followed by the token for that 182 00:14:32,260 --> 00:14:34,150 second user right here. 183 00:14:34,150 --> 00:14:42,310 That would be user to dot tokens grabbing the first one by its index and then grabbing the individual 184 00:14:42,370 --> 00:14:47,380 Jason web token and then we'll go ahead and send this one off to the server. 185 00:14:47,380 --> 00:14:54,550 Now for this case there's no need to send any data so we can move right along to our expected call and 186 00:14:54,550 --> 00:15:02,590 if there is no task found with that I.D. owned by that user we know we get a 4 0 4 back. 187 00:15:02,830 --> 00:15:07,140 So that takes care of everything in steps 1 and 2. 188 00:15:07,150 --> 00:15:14,110 Next up I want to make sure that task is still in the database down below what I'm gonna do is try to 189 00:15:14,110 --> 00:15:14,620 fetch it. 190 00:15:14,620 --> 00:15:16,330 Const task equals. 191 00:15:17,080 --> 00:15:21,460 I'll use a task and find by I.D. to fetch it. 192 00:15:21,670 --> 00:15:25,070 I have task one dot underscore I.D.. 193 00:15:25,360 --> 00:15:30,610 Now I am going to go ahead and use a wait with that and then we're just gonna make an assertion that 194 00:15:30,610 --> 00:15:32,410 we actually got a task back. 195 00:15:32,430 --> 00:15:38,310 So I will expect task not to be null. 196 00:15:38,490 --> 00:15:39,540 Perfect. 197 00:15:39,600 --> 00:15:41,940 Now with this in place we're all done. 198 00:15:41,940 --> 00:15:44,870 We are setting up everything including our assertions. 199 00:15:44,880 --> 00:15:51,050 I can remove the challenge comments as all there is left to do is to test our work. 200 00:15:51,180 --> 00:15:58,800 Right here we can see the first test suite has passed and then the second we have 13 passing test cases. 201 00:15:58,800 --> 00:16:03,960 Now at this point we have a pretty good test suite in place in the next lesson. 202 00:16:03,960 --> 00:16:09,870 We're going to wrap up this section on testing by going over some other test cases you could now right 203 00:16:10,080 --> 00:16:12,100 based off of what you now know. 204 00:16:12,120 --> 00:16:15,040 I'm excited to get to that so we'll see you in the next 1.