Class: Ape::EntryPostsValidator

Inherits:
Validator show all
Defined in:
lib/ape/validators/entry_posts_validator.rb

Instance Attribute Summary

Attributes inherited from Validator

#authent, #reporter

Instance Method Summary collapse

Methods inherited from Validator

custom_validators, instance

Methods included from Util

extended, included

Methods included from ValidatorDsl

included

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Ape::Validator

Instance Method Details

#check_new_entry(as_posted, new_entry, desc) ⇒ Object



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
186
187
188
# File 'lib/ape/validators/entry_posts_validator.rb', line 161

def check_new_entry(as_posted, new_entry, desc)

  if compare_entries(as_posted, new_entry, "entry as posted", desc)
    reporter.success(self, "#{desc} is consistent with posted entry.")
  end

  # * See if the categories we sent made it in
  cat_probs = false
  @cats.each do |cat|
    if !new_entry.has_cat(cat)
      cat_probs = true
      reporter.warning(self, "Provided category not in #{desc}: #{cat}")
    end
  end
  reporter.success(self, "Provided categories included in #{desc}.") unless cat_probs

  # * See if the dc:subject survived
  dc_subject = new_entry.child_content(Samples.foreign_child, Samples.foreign_namespace)
  if dc_subject
    if dc_subject == Samples.foreign_child_content
      reporter.success(self, "Server preserved foreign markup in #{desc}.")
    else
      reporter.warning(self, "Server altered content of foreign markup in #{desc}.")
    end
  else
    reporter.warning(self, "Server discarded foreign markup in #{desc}.")
  end
end

#compare1(e1, e2, e1Name, e2Name, field) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/ape/validators/entry_posts_validator.rb', line 198

def compare1(e1, e2, e1Name, e2Name, field)
  c1 = e1.child_content(field)
  c2 = e2.child_content(field)
  if c1 != c2
    problem = true
    if c1 == nil
      reporter.warning(self, "'#{field}' absent in #{e1Name}.")
    elsif c2 == nil
      reporter.warning(self, "'#{field}' absent in #{e2Name}.")
    else
      t1 = e1.child_type(field)
      t2 = e2.child_type(field)
      if t1 != t2
        reporter.warning(self, "'#{field}' has type='#{t1}' " +
          "in #{e1Name}, type='#{t2}' in #{e2Name}.")
      else
        c1 = Escaper.escape(c1)
        c2 = Escaper.escape(c2)
        reporter.warning(self, "'#{field}' in #{e1Name} [#{c1}] " +
          "differs from that in #{e2Name} [#{c2}].")
      end
    end
  end
  return problem
end

#compare_entries(e1, e2, e1Name, e2Name) ⇒ Object



190
191
192
193
194
195
196
# File 'lib/ape/validators/entry_posts_validator.rb', line 190

def compare_entries(e1, e2, e1Name, e2Name)
  problems = 0
  [ 'title', 'summary', 'content' ].each do |field|
    problems += 1 if compare1(e1, e2, e1Name, e2Name, field)
  end
  return problems == 0
end

#validate(opts = {}) ⇒ Object



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
# File 'lib/ape/validators/entry_posts_validator.rb', line 6

def validate(opts = {})
  entry_collection = opts[:entry_collection]
  reporter.info(self, "Will use collection '#{entry_collection.title}' for entry creation.")
  
  collection_uri = entry_collection.href
  entries = Feed.read(collection_uri, 'Entry collection', reporter)
  
  # * List the current entries, remember which IDs we've seen
  reporter.info(self, "TESTING: Entry-posting basics.")
  ids = []
  unless entries.empty?
    reporter.start_list(self, "Now in the Entries feed")
    entries.each do |entry|
      reporter.list_item(entry.summarize)
      ids << entry.child_content('id')
    end        
  end 
  
  # Setting up to post a new entry
  poster = Poster.new(collection_uri, @authent)
  if poster.last_error
    reporter.error(self, "Unacceptable URI for '#{entry_collection.title}' collection: " +
        poster.last_error)
    return
  end

  my_entry = Entry.new(:text => Samples.basic_entry)

  # ask it to use this in the URI
  slug_num = rand(100000)
  slug = "ape-#{slug_num}"
  slug_re = %r{ape.?#{slug_num}}
  poster.set_header('Slug', slug)

  # add some categories to the entry, and remember which
  @cats = Categories.add_cats(my_entry, entry_collection, @authent, reporter)

  # * OK, post it
  worked = poster.post(Names::AtomEntryMediaType, my_entry.to_s)
  name = 'Posting new entry'
  reporter.save_dialog(name, poster)
  if !worked
    reporter.error(self, "Can't POST new entry: #{poster.last_error}", name)
    return
  end

  location = poster.header('Location')
  unless location
    reporter.error(self, "No Location header upon POST creation", name)
    return
  end
  reporter.success(self, "Posting of new entry to the Entries collection " +
      "reported success, Location: #{location}", name)

  reporter.info(self, "Examining the new entry as returned in the POST response")
  check_new_entry(my_entry, poster.entry, "Returned entry") if poster.entry

  # * See if the Location uri can be retrieved, and check its consistency
  name = "Retrieval of newly created entry"
  new_entry = check_resource(location, name, Names::AtomMediaType)
  return unless new_entry

  # Grab its etag
  etag = new_entry.header 'etag'

  reporter.info(self, "Examining the new entry as retrieved using Location header in POST response:")

  begin
    new_entry = Entry.new(:text => new_entry.body, :uri => location)
  rescue REXML::ParseException
    prob = $!.to_s.gsub(/\n/, '<br/>')
    reporter.error(self, "New entry is not well-formed: #{prob}")
    return
  end

  # * See if the slug was used
  slug_used = false
  new_entry.alt_links.each do |a|
    href = a.attributes['href']
    if href && href.index(slug_re)
      slug_used = true
    end
  end
  if slug_used
    reporter.success(self, "Client-provided slug '#{slug}' was used in server-generated URI.")
  else
    reporter.warning(self, "Client-provided slug '#{slug}' not used in server-generated URI.")
  end

  check_new_entry(my_entry, new_entry, "Retrieved entry")

  entry_id = new_entry.child_content('id')

  # * fetch the feed again and check that version
  from_feed = find_entry(collection_uri, "entry collection", entry_id)
  if from_feed.class == String
    Feed.read(collection_uri, "Can't find entry in collection", reporter)
    reporter.error(self, "New entry didn't show up in the collections feed.")
    return
  end

  reporter.info(self, "Examining the new entry as it appears in the collection feed:")

  # * Check the entry from the feed
  check_new_entry(my_entry, from_feed, "Entry from collection feed")
 
  edit_uri = new_entry.link('edit', self)
  if !edit_uri
    reporter.error(self, "Entry from Location header has no edit link.")
    return
  end

  # * Update the entry, see if the update took
  name = 'In-place update with put'
  putter = Putter.new(edit_uri, @authent)

  # Conditional PUT if an etag
  putter.set_header('If-Match', etag) if etag

  new_title = "Let’s all do the Ape!"
  new_text = Samples.retitled_entry(new_title, entry_id)
  response = putter.put(Names::AtomEntryMediaType, new_text)
  reporter.save_dialog(name, putter)

  if response
    reporter.success(self, "Update of new entry reported success.", name)
    from_feed = find_entry(collection_uri, "entry collection", entry_id)
    if from_feed.class == String
      check_resource(collection_uri, "Check collection after lost update")
      reporter.error(self, "Updated entry ID #{entry_id} not found in entries collection.")
      return
    end
    if from_feed.child_content('title') == new_title
      reporter.success(self, "Title of new entry successfully updated.")
    else
      reporter.warning(self, "After PUT update of title, Expected " +
        "'#{new_title}', but saw '#{from_feed.child_content('title')}'")
    end
  else
    reporter.warning(self,"Can't update new entry with PUT: #{putter.last_error}", name)
  end

  # the edit-uri might have changed
  return unless delete_entry(from_feed, 'New Entry deletion')

  # See if it's gone from the feed
  still_there = find_entry(collection_uri, "entry collection", entry_id)
  if still_there.class != String
    reporter.error(self, "Entry is still in collection post-deletion.")
  else
    reporter.success(self, "Entry not found in feed after deletion.")
  end
  
end