defender 0.2.0 → 1.0.0beta1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +132 -0
- data/lib/defender.rb +16 -59
- data/lib/defender/document.rb +51 -385
- data/lib/defender/version.rb +3 -0
- metadata +54 -56
- data/.document +0 -5
- data/.gitignore +0 -23
- data/README.rdoc +0 -22
- data/Rakefile +0 -74
- data/VERSION +0 -1
- data/defender.gemspec +0 -70
- data/lib/defender/statistics.rb +0 -176
- data/spec/defender_spec.rb +0 -24
- data/spec/document_spec.rb +0 -155
- data/spec/spec.opts +0 -1
- data/spec/spec_helper.rb +0 -14
- data/spec/statistics_spec.rb +0 -37
data/spec/defender_spec.rb
DELETED
@@ -1,24 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
-
|
3
|
-
describe Defender do
|
4
|
-
before(:each) do
|
5
|
-
FakeWeb.clean_registry
|
6
|
-
end
|
7
|
-
|
8
|
-
context "api keys" do
|
9
|
-
it "returns false when given an invalid API key" do
|
10
|
-
FakeWeb.register_uri(:get, "http://api.defensio.com/2.0/users/foobar.json",
|
11
|
-
:body => '{"defensio-result":{"status":"failed","message":"API key not found","api-version":"2.0","owner-url":""}}',
|
12
|
-
:status => ['404', 'Not Found'])
|
13
|
-
Defender.api_key = "foobar"
|
14
|
-
Defender.check_api_key.should be_false
|
15
|
-
end
|
16
|
-
|
17
|
-
it "returns true when given a valid API key" do
|
18
|
-
FakeWeb.register_uri(:get, "http://api.defensio.com/2.0/users/barbaz.json",
|
19
|
-
:body => '{"defensio-result":{"status":"success","message":"","api-version":"2.0","owner-url":""}}')
|
20
|
-
Defender.api_key = "barbaz"
|
21
|
-
Defender.check_api_key.should be_true
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
data/spec/document_spec.rb
DELETED
@@ -1,155 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
-
|
3
|
-
describe Defender::Document do
|
4
|
-
before(:each) do
|
5
|
-
FakeWeb.clean_registry
|
6
|
-
end
|
7
|
-
|
8
|
-
context "creating documents" do
|
9
|
-
it "should allow an innocent document to be posted when given only required options" do
|
10
|
-
FakeWeb.register_uri(:post, "http://api.defensio.com/2.0/users/foobar/documents.json",
|
11
|
-
:body => '{"defensio-result":{"api-version":"2.0","status":"success","message":"","signature":"baz",
|
12
|
-
"allow":true,"classification":"innocent","spaminess":0.1,
|
13
|
-
"profanity-match":false}}')
|
14
|
-
Defender.api_key = "foobar"
|
15
|
-
document = Defender::Document.new
|
16
|
-
document.content = "[innocent,0.1]"
|
17
|
-
document.type = :test
|
18
|
-
document.save
|
19
|
-
|
20
|
-
document.allow?.should be_true
|
21
|
-
document.classification.should == "innocent"
|
22
|
-
document.spaminess.should == 0.1
|
23
|
-
document.profane?.should be_false
|
24
|
-
document.signature.should == "baz"
|
25
|
-
end
|
26
|
-
|
27
|
-
it "marks an asynchronously requested document as pending" do
|
28
|
-
FakeWeb.register_uri(:post, "http://api.defensio.com/2.0/users/foobar/documents.json",
|
29
|
-
:body => '{"defensio-result":{"api-version":"2.0","status":"pending","message":"","signature":"baz",
|
30
|
-
"allow":null,"classification":null,"spaminess":null,"profanity-match":null}}')
|
31
|
-
|
32
|
-
Defender.api_key = "foobar"
|
33
|
-
document = Defender::Document.new
|
34
|
-
document.content = "[innocent,0.1]"
|
35
|
-
document.type = :test
|
36
|
-
document.save(true)
|
37
|
-
|
38
|
-
document.pending?.should be_true
|
39
|
-
end
|
40
|
-
|
41
|
-
it "should not allow a spammy document to be posted when given only required options" do
|
42
|
-
FakeWeb.register_uri(:post, "http://api.defensio.com/2.0/users/foobar/documents.json",
|
43
|
-
:body => '{"defensio-result":{"api-version":"2.0","status":"success","message":"","signature":"bar",
|
44
|
-
"allow":false,"classification":"spam","spaminess":0.89,"profanity-match":false}}')
|
45
|
-
|
46
|
-
Defender.api_key = "foobar"
|
47
|
-
document = Defender::Document.new
|
48
|
-
document.content = "[spam,0.89]"
|
49
|
-
document.type = :test
|
50
|
-
document.save
|
51
|
-
|
52
|
-
document.allow?.should be_false
|
53
|
-
document.classification.should == "spam"
|
54
|
-
document.spaminess.should == 0.89
|
55
|
-
document.profane?.should be_false
|
56
|
-
document.signature.should == "bar"
|
57
|
-
end
|
58
|
-
|
59
|
-
it "accepts a string to parent-document-date" do
|
60
|
-
document = Defender::Document.new
|
61
|
-
document.parent_document_date = '1970-01-01'
|
62
|
-
document.attributes_hash['parent-document-date'].should == '1970-01-01'
|
63
|
-
end
|
64
|
-
|
65
|
-
it "accepts a Time to parent-document-date" do
|
66
|
-
time = Time.now
|
67
|
-
document = Defender::Document.new
|
68
|
-
document.parent_document_date = time
|
69
|
-
document.attributes_hash['parent-document-date'].should == time.strftime('%Y-%m-%d')
|
70
|
-
end
|
71
|
-
|
72
|
-
it "accepts a hash as headers" do
|
73
|
-
document = Defender::Document.new
|
74
|
-
document.http_headers = {"Foo" => "Bar", "Bar" => "Baz"}
|
75
|
-
document.attributes_hash['http-headers'].should == "Foo: Bar\nBar: Baz"
|
76
|
-
end
|
77
|
-
|
78
|
-
it "accepts an array as headers" do
|
79
|
-
document = Defender::Document.new
|
80
|
-
document.http_headers = ["Foo: Bar", "Bar: Baz"]
|
81
|
-
document.attributes_hash['http-headers'].should == "Foo: Bar\nBar: Baz"
|
82
|
-
end
|
83
|
-
|
84
|
-
it "returns false on server error" do
|
85
|
-
FakeWeb.register_uri(:post, "http://api.defensio.com/2.0/users/foobar/documents.json",
|
86
|
-
:body => '{"defensio-result":{"api-version":"2.0","status":"failed","message":"Oopsies"}}',
|
87
|
-
:status => ["500", "Server Error"])
|
88
|
-
Defender.api_key = "foobar"
|
89
|
-
document = Defender::Document.new
|
90
|
-
document.content = "[spam,0.89]"
|
91
|
-
document.type = :test
|
92
|
-
document.save.should be_false
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
context "finding documents" do
|
97
|
-
it "sets the attributes for a found object" do
|
98
|
-
FakeWeb.register_uri(:get, "http://api.defensio.com/2.0/users/foobar/documents/baz.json",
|
99
|
-
:body => '{"defensio-result":{"api-version":"2.0","status":"success","message":"",
|
100
|
-
"signature":"baz","allow":false,"classification":"spam","spaminess":0.89,"profanity-match":false}}')
|
101
|
-
|
102
|
-
Defender.api_key = "foobar"
|
103
|
-
document = Defender::Document.find("baz")
|
104
|
-
document.allow?.should be_false
|
105
|
-
document.classification.should == "spam"
|
106
|
-
document.spaminess.should == 0.89
|
107
|
-
document.profane?.should be_false
|
108
|
-
document.signature.should == "baz"
|
109
|
-
end
|
110
|
-
|
111
|
-
it "raises a StandardError on server error" do
|
112
|
-
FakeWeb.register_uri(:get, "http://api.defensio.com/2.0/users/foobar/documents/baz.json",
|
113
|
-
:body => '{"defensio-result":{"api-version":"2.0","status":"failed","message":"oops"}}',
|
114
|
-
:status => ["500", "Server Error"])
|
115
|
-
|
116
|
-
Defender.api_key = "foobar"
|
117
|
-
lambda { Defender::Document.find("baz") }.should raise_error(StandardError, "oops")
|
118
|
-
end
|
119
|
-
end
|
120
|
-
|
121
|
-
context "updating documents" do
|
122
|
-
it "only sets the allow attribute" do
|
123
|
-
FakeWeb.register_uri(:get, 'http://api.defensio.com/2.0/users/foobar/documents/baz.json',
|
124
|
-
:body => '{"defensio-result":{"api-version":"2.0","status":"success","message":"",
|
125
|
-
"signature":"baz","allow":false,"classification":"spam","spaminess":0.89,"profanity-match":false}}')
|
126
|
-
FakeWeb.register_uri(:put, 'http://api.defensio.com/2.0/users/foobar/documents/baz.json',
|
127
|
-
:body => '{"defensio-result":{"api-version":"2.0","status":"success","message":"",
|
128
|
-
"signature":"","allow":true,"classification":"spam","spaminess":0.89,"profanity-match":false"}}')
|
129
|
-
|
130
|
-
Defender.api_key = 'foobar'
|
131
|
-
document = Defender::Document.find('baz')
|
132
|
-
document.allow = true
|
133
|
-
oldcontent = document.content
|
134
|
-
lambda { document.content = 'foobar!' }.should raise_error(NameError)
|
135
|
-
document.save.should be_true
|
136
|
-
document.content.should == oldcontent
|
137
|
-
document.content.should_not == 'foobar!'
|
138
|
-
document.allow.should be_true
|
139
|
-
end
|
140
|
-
|
141
|
-
it 'returns false when the server encounts an error' do
|
142
|
-
FakeWeb.register_uri(:get, 'http://api.defensio.com/2.0/users/foobar/documents/baz.json',
|
143
|
-
:body => '{"defensio-result":{"api-version":"2.0","status":"success","message":"",
|
144
|
-
"signature":"baz","allow":false,"classification":"spam","spaminess":0.89,"profanity-match":false}}')
|
145
|
-
FakeWeb.register_uri(:put, 'http://api.defensio.com/2.0/users/foobar/documents/baz.json',
|
146
|
-
:body => '{"defensio-result":{"api-version":"2.0","status":"failed","message":"UTTER FAIL!"}}',
|
147
|
-
:status => ['500', 'Server Error'])
|
148
|
-
|
149
|
-
Defender.api_key = 'foobar'
|
150
|
-
document = Defender::Document.find('baz')
|
151
|
-
document.allow = true
|
152
|
-
document.save.should be_false
|
153
|
-
end
|
154
|
-
end
|
155
|
-
end
|
data/spec/spec.opts
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
--color
|
data/spec/spec_helper.rb
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
-
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
-
|
4
|
-
require 'rubygems'
|
5
|
-
|
6
|
-
require 'defender'
|
7
|
-
require 'spec'
|
8
|
-
require 'spec/autorun'
|
9
|
-
require 'fakeweb'
|
10
|
-
|
11
|
-
FakeWeb.allow_net_connect = false
|
12
|
-
|
13
|
-
Spec::Runner.configure do |config|
|
14
|
-
end
|
data/spec/statistics_spec.rb
DELETED
@@ -1,37 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
-
|
3
|
-
describe Defender::Statistics do
|
4
|
-
before(:each) do
|
5
|
-
FakeWeb.clean_registry
|
6
|
-
end
|
7
|
-
|
8
|
-
it "retrieves basic statistics from the server" do
|
9
|
-
FakeWeb.register_uri(:get, "http://api.defensio.com/2.0/users/foobar/basic-stats.json",
|
10
|
-
:body => '{"defensio-result":{"api-version":"2.0","status":"success","message":"",
|
11
|
-
"false-negatives":42,"false-positives":1,"learning":true,
|
12
|
-
"learning-status":"foo!","legitimate":{"total":15},
|
13
|
-
"recent-accuracy":0.9525,"unwanted":{"malicious":2,"spam":5,
|
14
|
-
"total":7}}}')
|
15
|
-
|
16
|
-
Defender.api_key = "foobar"
|
17
|
-
statistics = Defender::Statistics.new
|
18
|
-
statistics.api_version.should == "2.0"
|
19
|
-
statistics.false_negatives.should == 42
|
20
|
-
statistics.false_positives.should == 1
|
21
|
-
statistics.learning.should be_true
|
22
|
-
statistics.legitimate_total.should == 15
|
23
|
-
statistics.recent_accuracy.should == 0.9525
|
24
|
-
statistics.unwanted_malicious.should == 2
|
25
|
-
statistics.unwanted_spam.should == 5
|
26
|
-
statistics.unwanted_total.should == 7
|
27
|
-
end
|
28
|
-
|
29
|
-
it "raises a StandardError if the server fails" do
|
30
|
-
FakeWeb.register_uri(:get, "http://api.defensio.com/2.0/users/foobar/basic-stats.json",
|
31
|
-
:body => '{"defensio-result":{"api-version":"2.0","status":"failed","message":"Oops"}}',
|
32
|
-
:status => ["500", "Server Error"])
|
33
|
-
|
34
|
-
Defender.api_key = "foobar"
|
35
|
-
lambda { Defender::Statistics.new }.should raise_error(StandardError, "Oops")
|
36
|
-
end
|
37
|
-
end
|