File: posts_controller_test.rb

package info (click to toggle)
ruby-rabl 0.16.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,276 kB
  • sloc: ruby: 6,732; javascript: 102; makefile: 6
file content (185 lines) | stat: -rw-r--r-- 6,916 bytes parent folder | download
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# Lives in <rabl>/test/integration/posts_controller_test.rb
# Symlinked to fixture applications

begin # Padrino
  require File.expand_path(File.dirname(__FILE__) + '/../../test_config.rb')
rescue LoadError # Rails
  require File.expand_path(File.dirname(__FILE__) + '/../test_helper.rb')
end

require 'rexml/document'

context "PostsController" do
  helper(:json_output) { JSON.parse(last_response.body) }

  setup do
    create_users!
    Post.delete_all
    @post1 = Post.create(:title => "Foo", :body => "Bar", :user_id => @user1.id)
    @post2 = Post.create(:title => "Baz", :body => "Bah", :user_id => @user2.id)
    @post3 = Post.create(:title => "Kaz", :body => "<script>alert('xss & test');</script>", :user_id => @user3.id)
    @posts = [@post1, @post2, @post3]
  end

  context "for index action" do
    setup do
      get "/posts", format: :json
    end

    # Attributes (regular)
    asserts("contains post titles") do
      json_output['articles'].map { |o| o["article"]["title"] }
    end.equals { @posts.map(&:title) }

    asserts("contains post bodies") do
      json_output['articles'].map { |o| o["article"]["body"] }
    end.equals { @posts.map(&:body) }

    # Attributes (custom name)
    asserts("contains post posted_at") do
      json_output['articles'].map { |o| o["article"]["posted_at"] }
    end.equals { @posts.map(&:created_at).map{ |t| t.strftime("%Y-%m-%d %H:%M:%S %Z") } }

    # Child
    asserts("contains post user child username") do
      json_output['articles'].map { |o| o["article"]["user"]["username"] }
    end.equals { @posts.map(&:user).map(&:username) }

    asserts("contains post user child role") do
      json_output['articles'].map { |o| o["article"]["user"]["role"] }
    end.equals { ["normal", "normal", "admin"] }

    # Child Numbers of the Child User
    asserts("contains post user child numbers") do
      json_output['articles'].map { |o| o["article"]["user"]["pnumbers"][0]["pnumber"]["formatted"] }
    end.equals { @posts.map(&:user).map(&:phone_numbers).map(&:first).map(&:formatted) }

    # Glue (username to article)
    asserts("contains glued usernames") do
      json_output['articles'].map { |o| o["article"]["author_name"] }
    end.equals { @posts.map(&:user).map(&:username) }

    # Conditional Child (admin)
    asserts("contains admin child only for admins") do
      json_output['articles'].map { |o| o["article"]["admin"]["username"] if o["article"].has_key?("admin") }.compact
    end.equals { [@user3.username] }

    # Conditional Node (created_by_admin)
    asserts("contains created_by_admin node for admins") do
      json_output['articles'].last['article']['created_by_admin']
    end.equals { true }

    denies("contains no created_by_admin node for non-admins") do
      json_output['articles'].first['article']
    end.includes(:created_by_admin)
  end # index action, json

  context "escaping output in index action" do
    context "for first post" do
      setup do
        Rabl.configuration.escape_all_output = true
        get "/posts/#{@post1.id}", format: :json
        json_output['post']
      end

      # Attributes (regular)
      asserts("contains post title") { topic['title'] }.equals { @post1.title }
      asserts("contains post body")  { topic['body'] }.equals { @post1.body }
    end

    context "for third post with script tags" do
      setup do
        Rabl.configuration.escape_all_output = true
        get "/posts/#{@post3.id}", format: :json
        json_output['post']
      end

      # Attributes (regular)
      asserts("contains post title") { topic['title'] }.equals { @post3.title }
      asserts("contains escaped post body")  { topic['body'] }.equals { ERB::Util.h(@post3.body) }
    end
  end # escaping output

  context "for show action" do
    setup do
      get "/posts/#{@post1.id}", format: :json
      json_output['post']
    end

    # Attributes (regular)
    asserts("contains post title") { topic['title'] }.equals { @post1.title }
    asserts("contains post body")  { topic['body'] }.equals { @post1.body }

    # Attributes (custom name)
    asserts("contains post posted_at") { topic['posted_at'] }.equals { @post1.created_at.strftime("%Y-%m-%d %H:%M:%S %Z") }

    # Child
    asserts("contains post user child username") { topic["user"]["username"] }.equals { @post1.user.username }
    asserts("contains post user child role") { topic["user"]["role"] }.equals { "normal" }

    # Child Numbers of the Child User
    asserts("contains post user child numbers") do
      topic["user"]["pnumbers"][0]["pnumber"]["formatted"]
    end.equals { @post1.user.phone_numbers[0].formatted }

    # Glue (username to article)
    asserts("contains glued username") { topic["author_name"] }.equals { @post1.user.username }

    # Non-ORM Date Node Partial
    context "for date node" do
      setup { json_output['post']['created_date'] }
      asserts("contains date partial with day")   { topic['day'] }.equals { @post1.created_at.day }
      asserts("contains date partial with hour")  { topic['hour'] }.equals { @post1.created_at.hour }
      asserts("contains date partial with full")  { topic['full'] }.equals { @post1.created_at.iso8601 }
    end # date node

    asserts("contains helper action") { topic["foo"] }.equals { "BAR!" }
    denies("contains helper action") { topic["created_at_in_words"] }.nil

    asserts("contains post attributes via node") { topic["post"] }.equals { [@post1.title, @post1.body] }
  end # show action, json

  context "renderer" do
    setup do
      mock(ActionController::Base).perform_caching.any_number_of_times { true }
      get "/posts/#{@post1.id}/renderer"
      json_output['post']
    end

    # Attributes (regular)
    asserts("contains post title") { topic['title'] }.equals { @post1.title }
    asserts("contains post body")  { topic['body'] }.equals { @post1.body }

    # Attributes (partial)
    asserts("contains post partial title") { topic['partial']['title'] }.equals { @post1.title }
    asserts("contains post partial body")  { topic['partial']['body'] }.equals { @post1.body }
  end # renderer action, json

  # HTML isn't supported in API mode
  unless Rails.application.config.api_only
    context "for index action rendering JSON within HTML" do
      setup do
        get "/posts", format: :html
      end

      asserts(:body).includes { "<html>" }
    end # index action, html

    context "for show action rendering JSON within HTML" do
      setup do
        get "/posts/#{@post1.id}", format: :html
      end

      asserts(:body).includes { "<html>" }
    end # show action, html
  end

  context "mime_type" do
    setup do
      get "/posts/#{@post1.id}", format: :rabl_test_v1
    end

    asserts("contains post title") { json_output['post']['title_v1'] }.equals { @post1.title }
    asserts("contains username") { json_output['post']['user']['username_v1'] }.equals { @post1.user.username }
  end
end