~tim/wuparty

ref: 95ffe695fa4a525f586b31d46c007c2d29ce67e8 wuparty/test/wuparty_test.rb -rw-r--r-- 3.4 KiB
95ffe695Tim Morgan Fix tests, make a rake task. 8 years ago
                                                                                
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
require './lib/wuparty'
require 'test/unit'

class WuPartyTest < Test::Unit::TestCase

  # Must create a form called "Test Form" and pass in its id
  # via the ENV variable WUFOO_FORM_ID.
  # Give the form standard name and address fields.
  # Make the name field required.

  def setup
    if ENV['WUFOO_ACCOUNT'] and ENV['WUFOO_API_KEY'] and ENV['WUFOO_FORM_ID']
      @wufoo = WuParty.new(ENV['WUFOO_ACCOUNT'], ENV['WUFOO_API_KEY'])
      @form_id = ENV['WUFOO_FORM_ID']
    else
      puts 'Must set WUFOO_ACCOUNT, WUFOO_API_KEY and WUFOO_FORM_ID env variables before running.'
      exit(1)
    end
  end

  def test_forms
    assert @wufoo.forms
  end

  def test_form
    form = @wufoo.form(@form_id)
    assert form
    assert_equal 'Test Form', form['Name']
  end

  def test_form_by_hash
    hash = @wufoo.form(@form_id)['Hash']
    assert @wufoo.form(hash)
  end

  def test_form_directly
    form = WuParty::Form.new(@form_id, :account => ENV['WUFOO_ACCOUNT'], :api_key => ENV['WUFOO_API_KEY'])
    assert_equal 'Test Form', form['Name']
  end

  def test_non_existent_form
    assert_raise WuParty::HTTPError do
      @wufoo.form('does-not-exist')
    end
  end

  def test_reports
    assert @wufoo.reports
  end

  def test_users
    assert @wufoo.users
  end

  def test_form_fields
    form = @wufoo.form(@form_id)
    field_names = form.fields.map { |f| f['Title'] }
    assert field_names.include?('Name'), 'Name field not found in #fields'
    assert field_names.include?('Address'), 'Address field not found in #fields'
  end

  def test_form_submit
    form = @wufoo.form(@form_id)
    result = form.submit('Field1' => 'Tim', 'Field2' => 'Morgan', 'Field3' => '4010 W. New Orleans', 'Field5' => 'Broken Arrow', 'Field6' => 'OK', 'Field7' => '74011')
    assert_equal 1, result['Success']
    assert result['EntryId']
    assert result['EntryLink']
  end

  def test_form_submit_error
    form = @wufoo.form(@form_id)
    # test a form error -- non-existent field
    result = form.submit('Field1' => 'Tim', 'Field2' => 'Morgan', 'Field100' => 'Foobar')
    assert_equal 0, result['Success']
    assert result['ErrorText']
    assert_equal [], result['FieldErrors']
    # test a field error -- nothing in a required field
    result = form.submit('Field2' => 'Morgan')
    assert_equal 0, result['Success']
    assert_equal 1, result['FieldErrors'].length
    error = result['FieldErrors'].first
    assert_equal 'Field1', error['ID']
    assert_match /required/i, error['ErrorText']
  end

  def test_entries
    form = @wufoo.form(@form_id)
    form.submit('Field1' => 'Tim', 'Field2' => 'Morgan')
    assert_equal 'Tim', form.entries.last['Field1']
  end

  def test_filtering_entries
    form = @wufoo.form(@form_id)
    form.submit('Field1' => 'Tim', 'Field2' => 'Morgan')
    id = form.submit('Field1' => 'Jane', 'Field2' => 'Smith')['EntryId']
    assert form.entries([['Field2', 'Is_equal_to', 'Morgan']]).any?
    assert_equal 1, form.entries([['EntryId', 'Is_equal_to', id]]).length
  end
  
  def test_add_webhook
    # test with optional parameters
    response = @wufoo.add_webhook(@form_id, "http://#{ENV['WUFOO_ACCOUNT']}.com/#{@form_id}", true, "handshakeKey01")
    assert_match /[a-z0-9]{6}/i, response['WebHookPutResult']['Hash']
    # test without optional parameters
    response = @wufoo.add_webhook(@form_id, "http://#{ENV['WUFOO_ACCOUNT']}.com/#{@form_id}-2")
    assert_match /[a-z0-9]{6}/i, response['WebHookPutResult']['Hash']
  end

end