Skip to content
This repository has been archived by the owner on Jan 29, 2022. It is now read-only.

Add tests for the DataParsers helper module #55

Merged
merged 2 commits into from
Jun 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions lib/helpers/data_parsers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,12 @@ def sanitize_input(input_string)
sanitized
end

def normalize(str)
settings.allow_uppercase ? str : str.downcase
def normalize(allow_upper, str)
allow_upper ? str : str.downcase
end

def verify_signature(payload_body)
signature = 'sha1=' + OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha1'), settings.github_secret, payload_body)
def verify_signature(secret, payload_body)
signature = 'sha1=' + OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha1'), secret, payload_body)
throw(:halt, [500, "Signatures didn't match!\n"]) unless Rack::Utils.secure_compare(signature, request.env['HTTP_X_HUB_SIGNATURE'])
end

def payload
env['parsed_body']
end
end
4 changes: 2 additions & 2 deletions lib/routes/module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ def self.registered(puppet_webhook)

# TODO: Move these two lines of code into the parser
decoded = request.body.read
verify_signature(decoded) if verify_signature?
verify_signature(settings.github_secret, decoded) if verify_signature?

module_name = payload[:module_name]
module_name = env['parsed_body'][:module_name]

module_name = sanitize_input(module_name)
LOGGER.info("Deploying module #{module_name}")
Expand Down
14 changes: 7 additions & 7 deletions lib/routes/payload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ def self.registered(puppet_webhook)
else
request.body.read
end
verify_signature(decoded) if verify_signature?
verify_signature(settings.github_secret, decoded) if verify_signature?
data = JSON.parse(decoded, quirks_mode: true)

# Iterate the data structure to determine what's should be deployed
branch = payload[:branch]
branch = env['parsed_body'][:branch]

# If prefix is enabled in our config file, determine what the prefix should be
prefix = case settings.prefix
when :repo
payload[:repo_name]
env['parsed_body'][:repo_name]
when :user
payload[:repo_user]
env['parsed_body'][:repo_user]
when :command, TrueClass
run_prefix_command(data.to_json)
when String
Expand All @@ -39,7 +39,7 @@ def self.registered(puppet_webhook)

# When a branch is being deleted, a deploy against it will result in a failure, as it no longer exists.
# Instead, deploy the default branch, which will purge deleted branches per the user's configuration
deleted = payload[:deleted]
deleted = env['parsed_body'][:deleted]

branch = if deleted
settings.default_branch
Expand All @@ -51,9 +51,9 @@ def self.registered(puppet_webhook)
# The best we can do is just deploy all environments by passing nil to
# deploy() if we don't know the correct branch.
env = if prefix.nil? || prefix.empty? || branch.nil? || branch.empty?
normalize(branch)
normalize(settings.allow_uppercase, branch)
else
normalize("#{prefix}_#{branch}")
normalize(settings.allow_uppercase, "#{prefix}_#{branch}")
end

if ignore_env?(env)
Expand Down
19 changes: 11 additions & 8 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@

require File.expand_path '../lib/puppet_webhook.rb', __dir__

def app
PuppetWebhook
end

def call(env)
app.call(env)
end

module Webhook
module Test
module Methods
Expand All @@ -33,13 +41,8 @@ def read_json_fixture(name)
end
end

module RSpecMixin
include Rack::Test::Methods
include Webhook::Test::Methods
def app
described_class
end
end
RSpec.configure do |conf|
conf.include RSpecMixin
conf.include Rack::Test::Methods
conf.include Webhook::Test::Methods
conf.include DataParsers
end
36 changes: 36 additions & 0 deletions spec/unit/helpers/data_parsers_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require 'spec_helper'
require 'logger'

describe DataParsers do
subject do
Class.new do
include DataParsers
end
end

describe '#sanitize_input' do
it 'should sanitize and escape characters in a module or branch name' do
stub_const('LOGGER', Logger.new(nil))
expect(subject.new.sanitize_input('$mybranch')).to eq('\\$mybranch')
end

it 'should not sanitize the module or branch name' do
stub_const('LOGGER', Logger.new(nil))
expect(subject.new.sanitize_input('module_name')).to eq('module_name')
end
end

describe '#normalize' do
it 'should take in a string with uppercase and not change it' do
t = 'AllowUpperCaseStrings'
upper = true
expect(subject.new.normalize(upper, t)).to eq(t)
end

it 'should take in a string with uppercase and normalize it' do
t = 'AllowUpperCaseStrings'
upper = false
expect(subject.new.normalize(upper, t)).to eq('allowuppercasestrings')
end
end
end
39 changes: 39 additions & 0 deletions spec/unit/plugins/webhook_mcollective_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,28 @@
require 'mcollective'

describe PuppetWebhook::Mcollective do
describe '#initialize' do
it 'should create a new object' do
mcollective_object = PuppetWebhook::Mcollective.new('r10k',
'deploy',
{
timeout: 120,
dtimeout: 10
},
environment: 'production')
expect(mcollective_object.instance_variable_get('@agent')).to eq('r10k')
expect(mcollective_object.instance_variable_get('@command')).to eq('deploy')
expect(mcollective_object.instance_variable_get('@timeout')).to eq(120)
expect(mcollective_object.instance_variable_get('@dtimeout')).to eq(10)
expect(mcollective_object.instance_variable_get('@args')).to eq(environment: 'production')
end
end

describe '#run' do
before(:each) do
allow(PuppetWebhook::Mcollective).to receive(:new).and_return(nil)
end

it 'should run correctly' do
result_data = {
agent: 'puppet',
Expand All @@ -23,4 +41,25 @@
expect(PuppetWebhook::Mcollective.run).to eq(result_data)
end
end

describe '#client' do
context 'expect return of a client object' do
before(:each) do
allow(PuppetWebhook::Mcollective).to receive(:new).and_return(nil)
allow(MCollective::RPC::Client).to receive(:new).and_return(nil)
end

it 'is expected to return a client to send commands to' do
client_data = {
agent: 'r10k',
timeout: 120,
discovery_timeout: 10,
progress: false
}

allow(PuppetWebhook::Mcollective).to receive(:client).and_return(client_data)
expect(PuppetWebhook::Mcollective.client).to eq(client_data)
end
end
end
end