-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_test.rb
120 lines (95 loc) · 4.71 KB
/
app_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env ruby
require 'minitest/autorun'
require 'rack/test'
require_relative "app"
set :enviroment, :test
class MyAppTest < MiniTest::Test
include Rack::Test::Methods
def app
Sinatra::Application
end
def setup
User.delete_all(email: '[email protected]')
@user_uuid = SecureRandom.uuid
post '/user', body = { 'id' => @user_uuid, 'fname' => 'Alex' , 'lname' => 'Smith', 'email' => '[email protected]', 'password' => 'pwd' }.to_json
assert last_response.ok? , 'Error in POST /user response'
post '/user/auth', body = { 'email' => '[email protected]', 'password' => 'pwd' }.to_json
assert last_response.ok? , 'Error in POST /user/auth response'
Category.delete_all(name: 'Furniture')
@category_uuid = SecureRandom.uuid
post '/category', body = { 'id' => @category_uuid, 'name' => 'Furniture' }.to_json
assert last_response.ok? , 'Error in POST /category'
Post.delete_all(title: 'New Bike for sale')
end
## Posts
def test_post
post_uuid = SecureRandom.uuid
post '/post', body = { 'id' => post_uuid, 'user_id' => @user_uuid, 'title' => 'Bike for sale', 'description' => 'New bike', 'category_id' => @category_uuid, 'cost' => '10' }.to_json
assert last_response.ok?, 'Error in POST /post response'
put "/post/id/#{post_uuid}" , body = { 'title' => 'New Bike for sale', 'description' => 'One New bike', 'cost' => '20' }.to_json
assert last_response.ok?, 'Error in PUT /post/id response'
get "/post/id/#{post_uuid}"
assert last_response.ok?, 'Error in GET /post/id response'
test_post = last_response.body
test_post = JSON.parse(test_post)
assert test_post['id'] == post_uuid, 'Error in GET /post/id'
assert test_post['user']['id'] == @user_uuid, 'Error in GET /post/id'
assert test_post['title'] == 'New Bike for sale', 'Error in GET /post/id'
assert test_post['description'] == 'One New bike', 'Error in GET /post/id'
assert Date.parse(test_post['date']) == Date.parse(Time.now.utc.to_s), 'Incorrect date for post'
assert test_post['category']['id'] == @category_uuid, 'Error in GET /post/id'
assert_nil test_post['user']['password'], 'wrongly returns password'
assert test_post['cost'] == 20, 'Error in GET /post/id'
get '/post/all'
assert last_response.ok?, 'Error in GET /post/all response'
all_posts = last_response.body
all_posts = JSON.parse(all_posts)
assert all_posts.include?(test_post), 'Error in GET /post/all does not include newly added post'
delete "/post/id/#{post_uuid}"
assert last_response.ok?, 'Error in DELETE /post/id response'
get '/post/all'
all_posts = last_response.body
all_posts = JSON.parse(all_posts)
assert !(all_posts.include? test_post), 'Error in DELETE /post/id does not remove post'
end
## Categories
def test_category
get "/category/id/#{@category_uuid}"
assert last_response.ok?, 'Error in GET /category/id response'
test_category = last_response.body
test_category = JSON.parse(test_category)
assert test_category['id'] == @category_uuid, 'Error in GET /category/id'
assert test_category['name'] == 'Furniture', 'Error in GET /category/id'
get '/category/all'
assert last_response.ok? , 'Error in GET /category/all response'
all_categories = last_response.body
all_categories = JSON.parse(all_categories)
assert all_categories.include?(test_category), 'Error in GET /category/all does not include newly added Category'
end
## Users
def test_user
put "/user/id/#{@user_uuid}", body = {'fname' => 'Alexander' , 'lname' => 'Smithy'}.to_json
assert last_response.ok? , 'Error in PUT /user/id response'
get "/user/id/#{@user_uuid}"
assert last_response.ok? , 'Error in GET /user/id response'
test_user = last_response.body
test_user = JSON.parse(test_user)
assert test_user['id'] == @user_uuid, 'Error in GET /user/id'
assert test_user['fname'] == 'Alexander', 'Error in GET /user/id'
assert test_user['lname'] == 'Smithy', 'Error in GET /user/id'
assert_nil test_user['password'], 'Wrongly returns password'
get '/user/all'
assert last_response.ok? , 'Error in GET /user/all response'
all_users = last_response.body
all_users = JSON.parse(all_users)
assert all_users.include?(test_user), 'Error in GET /user/all does not include newly added user'
delete "/user/id/#{@user_uuid}"
assert last_response.ok? , 'Error in DELETE /user/id response'
get '/user/all'
all_users = last_response.body
all_users = JSON.parse(all_users)
assert !(all_users.include? test_user), 'Error in DELETE /user/id does not remove user'
post '/user/deauth'
assert last_response.ok? , 'Error in POST /user/deauth response'
end
end