File: client_test.rb

package info (click to toggle)
ruby-akismet 2.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 124 kB
  • ctags: 62
  • sloc: ruby: 380; makefile: 3
file content (158 lines) | stat: -rw-r--r-- 3,952 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
require 'test_helper'
require 'date'

class ClientTest < Test

  APP_URL = 'http://example.com'
  APP_NAME = 'Akismet tests'

  def setup
    @client = Akismet::Client.new( API_KEY,
      APP_URL,
      app_name: APP_NAME,
      app_version: Akismet::VERSION )

    @invalid_client = Akismet::Client.new( 'invalid-api-key',
      APP_URL,
      app_name: APP_NAME,
      app_version: Akismet::VERSION )
  end

  def test_attrs
    assert_equal @client.api_key, API_KEY
    assert_equal @client.app_url, APP_URL
    assert_equal @client.app_name, 'Akismet tests'
    assert_equal @client.app_version, Akismet::VERSION
  end

  def test_verify_key_with_valid_key_returns_true
    assert_equal true, @client.verify_key
  end

  def test_verify_key_with_invalid_key_returns_false
    assert_equal false, @invalid_client.verify_key
  end

  def test_check_with_invalid_api_key_raises
    assert_raises( Akismet::Error ) do
      @invalid_client.comment_check 'ip', 'ua'
    end
  end

  # Akismet returns true when author == 'viagra-test-123'
  def test_check_with_spam_returns_true
    spam, _ = @client.check('ip', 'ua', author: 'viagra-test-123')
    assert_equal true, spam
  end

  # Akismet returns false when user_role == 'administrator'
  def test_check_with_ham_returns_false
    spam, _ = @client.check('ip', 'ua', user_role: 'administrator')
    assert_equal false, spam
  end

  def test_check_with_all_params_succeeds
    @client.check 'ip', 'ua',
      type: 'comment',
      text: 'hello',
      created_at: DateTime.now,
      author: 'author',
      author_url: 'http://example.com',
      author_email: 'joe@example.com',
      post_url: 'http://example.com/posts/1',
      post_modified_at: DateTime.now,
      languages: %w{en fr},
      referrer: 'http://example.com',
      env: {a: 1, b: 1},
      user_role: 'Administrator',
      test: true
  end

  # Akismet returns true when author == 'viagra-test-123'
  def test_spam_predicate_with_spam_returns_true
    assert_equal true, @client.spam?('ip', 'ua', author: 'viagra-test-123')
  end

  # Akismet returns false when user_role == 'administrator'
  def test_spam_predicate_with_ham_returns_false
    assert_equal false, @client.spam?('ip', 'ua', user_role: 'administrator')
  end

  def test_spam_predicate_with_invalid_api_key_raises
    assert_raises( Akismet::Error ) do
      @invalid_client.spam? 'ip', 'ua'
    end
  end

  def test_ham_succeeds
    @client.ham 'ip', 'ua', text: 'hello'
  end

  def test_ham_with_invalid_api_key_raises
    assert_raises( Akismet::Error ) do
      @invalid_client.ham 'ip', 'ua'
    end
  end

  def test_spam_succeeds
    @client.spam 'ip', 'ua', test: 'hello'
  end

  def test_spam_with_invalid_api_key_raises
    assert_raises( Akismet::Error ) do
      @invalid_client.spam 'ip', 'ua'
    end
  end

  def test_open_opens_client
    refute @client.open?
    @client.open
    assert @client.open?
    @client.close
  end

  def test_open_with_block_opens_then_closes_client
    refute @client.open?
    @client.open { assert @client.open? }
    refute @client.open?
  end

  def test_open_raises_when_client_open
    assert !@client.open?
    @client.open
    assert @client.open?
    assert_raises( RuntimeError ) { @client.open }
  end

  def test_close_closes_client
    @client.open
    assert @client.open?
    @client.close
    refute @client.open?
  end

  def test_close_succeeds_when_client_closed
    assert !@client.open?
    @client.close
  end

  def test_class_open_yields_open_client
    Akismet::Client.open( API_KEY, APP_URL ) do |client|
      assert client.is_a?( Akismet::Client )
      assert client.open?
    end
  end

  def test_conflicting_env_var_raises
    assert_raises(ArgumentError) do
      @client.check 'ip', 'ua', env: { referrer: 'referrer' }
    end
  end

  def test_invalid_param_raises
    assert_raises(ArgumentError) do
      @client.check 'ip', 'ua', invalid_param: 'invalid'
    end
  end

end