Skip to content

Commit

Permalink
Adds the latest_version, last_updated and popular? attributes - Ref #853
Browse files Browse the repository at this point in the history
  • Loading branch information
erwanlr committed Sep 6, 2015
1 parent c03a44d commit fd0c47f
Show file tree
Hide file tree
Showing 16 changed files with 77 additions and 97 deletions.
23 changes: 22 additions & 1 deletion lib/common/models/wp_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class WpItem
# @return [ Array ]
# Make it private ?
def allowed_options
[:name, :wp_content_dir, :wp_plugins_dir, :path, :version, :vulns_file]
[:name, :wp_content_dir, :wp_plugins_dir, :path, :version, :db_file]
end

# @param [ URI ] target_base_uri
Expand All @@ -37,6 +37,27 @@ def initialize(target_base_uri, options = {})
forge_uri(target_base_uri)
end

def identifier
@identifier ||= name
end

# @return [ Hash ]
def db_data
@db_data ||= json(db_file)[identifier] || {}
end

def latest_version
db_data['latest_version']
end

def last_updated
db_data['last_ipdated']
end

def popular?
db_data['popular']
end

# @param [ Hash ] options
#
# @return [ void ]
Expand Down
4 changes: 2 additions & 2 deletions lib/common/models/wp_item/output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ module Output
def output(verbose = false)
puts
puts info("Name: #{self}") #this will also output the version number if detected
puts " | Latest version:"
puts " | Last updated:"
puts " | Latest version: #{latest_version}" if latest_version
puts " | Last updated: #{last_updated}" if last_updated
puts " | Location: #{url}"
#puts " | WordPress: #{wordpress_url}" if wordpress_org_item?
puts " | Readme: #{readme_url}" if has_readme?
Expand Down
13 changes: 6 additions & 7 deletions lib/common/models/wp_item/vulnerable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@

class WpItem
module Vulnerable
attr_accessor :vulns_file, :identifier
attr_accessor :db_file, :identifier

# Get the vulnerabilities associated to the WpItem
# Filters out already fixed vulnerabilities
#
# @return [ Vulnerabilities ]
def vulnerabilities
json = json(vulns_file)
vulnerabilities = Vulnerabilities.new
return @vulnerabilities if @vulnerabilities

return vulnerabilities if json.empty?
@vulnerabilities = Vulnerabilities.new

json[identifier]['vulnerabilities'].each do |vulnerability|
[*db_data['vulnerabilities']].each do |vulnerability|
vulnerability = Vulnerability.load_from_json_item(vulnerability)
vulnerabilities << vulnerability if vulnerable_to?(vulnerability)
@vulnerabilities << vulnerability if vulnerable_to?(vulnerability)
end

vulnerabilities
@vulnerabilities
end

def vulnerable?
Expand Down
7 changes: 3 additions & 4 deletions lib/common/models/wp_plugin.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
# encoding: UTF-8

require 'wp_plugin/vulnerable'

class WpPlugin < WpItem
include WpPlugin::Vulnerable

# Sets the @uri
#
# @param [ URI ] target_base_uri The URI of the wordpress blog
Expand All @@ -14,4 +10,7 @@ def forge_uri(target_base_uri)
@uri = target_base_uri.merge(URI.encode(wp_plugins_dir + '/' + name + '/'))
end

def db_file
@db_file ||= PLUGINS_FILE
end
end
15 changes: 0 additions & 15 deletions lib/common/models/wp_plugin/vulnerable.rb

This file was deleted.

5 changes: 3 additions & 2 deletions lib/common/models/wp_theme.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

require 'wp_theme/findable'
require 'wp_theme/versionable'
require 'wp_theme/vulnerable'
require 'wp_theme/info'
require 'wp_theme/output'
require 'wp_theme/childtheme'

class WpTheme < WpItem
extend WpTheme::Findable
include WpTheme::Versionable
include WpTheme::Vulnerable
include WpTheme::Info
include WpTheme::Output
include WpTheme::Childtheme
Expand All @@ -33,4 +31,7 @@ def style_url
@uri.merge('style.css').to_s
end

def db_file
@db_file ||= THEMES_FILE
end
end
15 changes: 0 additions & 15 deletions lib/common/models/wp_theme/vulnerable.rb

This file was deleted.

11 changes: 8 additions & 3 deletions lib/common/models/wp_version.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
# encoding: UTF-8

require 'wp_version/findable'
require 'wp_version/vulnerable'
require 'wp_version/output'

class WpVersion < WpItem

extend WpVersion::Findable
include WpVersion::Vulnerable
include WpVersion::Output

# The version number
Expand All @@ -17,6 +14,14 @@ class WpVersion < WpItem
# @return [ Array ]
def allowed_options; super << :number << :found_from end

def identifier
@identifier ||= number
end

def db_file
@db_file ||= WORDPRESSES_FILE
end

# @param [ WpVersion ] other
#
# @return [ Boolean ]
Expand Down
15 changes: 0 additions & 15 deletions lib/common/models/wp_version/vulnerable.rb

This file was deleted.

2 changes: 1 addition & 1 deletion spec/lib/common/models/wp_item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
end
it_behaves_like 'WpItem::Versionable'
it_behaves_like 'WpItem::Vulnerable' do
let(:vulns_file) { MODELS_FIXTURES + '/wp_item/vulnerable/items_vulns.json' }
let(:db_file) { MODELS_FIXTURES + '/wp_item/vulnerable/items_vulns.json' }
let(:identifier) { 'neo' }
let(:expected_refs) { {
'id' => [2993],
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/common/models/wp_plugin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
describe WpPlugin do
it_behaves_like 'WpPlugin::Vulnerable'
it_behaves_like 'WpItem::Vulnerable' do
let(:options) { { name: 'white-rabbit' } }
let(:vulns_file) { MODELS_FIXTURES + '/wp_plugin/vulnerable/plugins.json' }
let(:options) { { name: 'white-rabbit' } }
let(:db_file) { MODELS_FIXTURES + '/wp_plugin/vulnerable/plugins.json' }
let(:expected_refs) { {
'id' => [2993],
'url' => ['Ref 1', 'Ref 2'],
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/common/models/wp_theme_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
it_behaves_like 'WpTheme::Vulnerable'
it_behaves_like 'WpItem::Vulnerable' do
let(:options) { { name: 'the-oracle' } }
let(:vulns_file) { MODELS_FIXTURES + '/wp_theme/vulnerable/themes_vulns.json' }
let(:db_file) { MODELS_FIXTURES + '/wp_theme/vulnerable/themes_vulns.json' }
let(:expected_refs) { {
'id' => [2993],
'url' => ['Ref 1', 'Ref 2'],
Expand Down
16 changes: 8 additions & 8 deletions spec/shared_examples/wp_item_vulnerable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
shared_examples 'WpItem::Vulnerable' do

# 2 variables have to be set in the described class or subject:
# let(:vulns_file) { }
# let(:expected_vulns) { } The expected Vulnerabilities when using vulns_file and vulns_xpath
# let(:db_file) { }
# let(:expected_vulns) { } The expected Vulnerabilities when using db_file and vulns_xpath
#
# 1 variable is optional, used if supplied, otherwise subject.vulns_xpath is used
# let(:vulns_xpath) { }
Expand All @@ -18,24 +18,24 @@
end

after do
subject.vulns_file = @vulns_file
subject.db_file = @db_file
subject.identifier = identifier if defined?(identifier)

result = subject.vulnerabilities
expect(result).to be_a Vulnerabilities
expect(result).to eq @expected
end

context 'when the vulns_file is empty' do
context 'when the db_file is empty' do
it 'returns an empty Vulnerabilities' do
@vulns_file = empty_file
@expected = Vulnerabilities.new
@db_file = empty_file
@expected = Vulnerabilities.new
end
end

it 'returns the expected vulnerabilities' do
@vulns_file = vulns_file
@expected = expected_vulns
@db_file = db_file
@expected = expected_vulns
end
end

Expand Down
14 changes: 7 additions & 7 deletions spec/shared_examples/wp_plugin_vulnerable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@

shared_examples 'WpPlugin::Vulnerable' do

describe '#vulns_file' do
after { expect(subject.vulns_file).to eq @expected }
describe '#db_file' do
after { expect(subject.db_file).to eq @expected }

context 'when :vulns_file is no set' do
context 'when :db_file is no set' do
it 'returns the default one' do
@expected = PLUGINS_FILE
end
end

context 'when the :vulns_file is already set' do
context 'when the :db_file is already set' do
it 'returns it' do
@expected = 'test.json'
subject.vulns_file = @expected
@expected = 'test.json'
subject.db_file = @expected
end
end
end

describe '#identifier' do
its(:identifier) { is_expected.to eq 'plugin-name' }
its(:identifier) { should eq 'plugin-name' }
end

end
14 changes: 7 additions & 7 deletions spec/shared_examples/wp_theme_vulnerable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@

shared_examples 'WpTheme::Vulnerable' do

describe '#vulns_file' do
after { expect(subject.vulns_file).to eq @expected }
describe '#db_file' do
after { expect(subject.db_file).to eq @expected }

context 'when :vulns_file is not set' do
context 'when :db_file is not set' do
it 'returns the default one' do
@expected = THEMES_FILE
end
end

context 'when the :vulns_file is already set' do
context 'when the :db_file is already set' do
it 'returns it' do
@expected = 'test.json'
subject.vulns_file = @expected
@expected = 'test.json'
subject.db_file = @expected
end
end
end

describe '#identifier' do
its(:identifier) { is_expected.to eq 'theme-name' }
its(:identifier) { should eq 'theme-name' }
end

end
14 changes: 7 additions & 7 deletions spec/shared_examples/wp_version_vulnerable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@

shared_examples 'WpVersion::Vulnerable' do

describe '#vulns_file' do
after { expect(subject.vulns_file).to eq @expected }
describe '#db_file' do
after { expect(subject.db_file).to eq @expected }

context 'when :vulns_file is no set' do
context 'when :db_file is no set' do
it 'returns the default one' do
@expected = WORDPRESSES_FILE
end
end

context 'when the :vulns_file is already set' do
context 'when the :db_file is already set' do
it 'returns it' do
@expected = 'test.json'
subject.vulns_file = @expected
@expected = 'test.json'
subject.db_file = @expected
end
end
end

describe '#identifier' do
its(:identifier) { is_expected.to eq '1.2' }
its(:identifier) { should eq '1.2' }
end

end

0 comments on commit fd0c47f

Please sign in to comment.