Skip to content

Commit

Permalink
Add wemock to more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
greeneca committed May 24, 2017
1 parent 921b674 commit fab4721
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 74 deletions.
8 changes: 8 additions & 0 deletions lib/roku_builder/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ def configure
end
end

def root_dir=(root_dir)
@parsed[:root_dir] = root_dir
end

def method_missing(method)
@parsed[method]
end

private

def check_config_file
Expand Down
2 changes: 2 additions & 0 deletions lib/roku_builder/keyer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ def genkey(out_file: nil)
end

Dir.mktmpdir { |dir|
config_copy = @config.dup
config_copy.root_dir = dir
Manifest.generate({config: @config, attributes: {}})
Dir.mkdir(File.join(dir, "source"))
File.open(File.join(dir, "source", "main.brs"), "w") do |io|
Expand Down
9 changes: 9 additions & 0 deletions test/roku_builder/test_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,5 +167,14 @@ def test_config_configure_edit_params_default
end
File.delete(target_config) if File.exist?(target_config)
end

def test_config_set_root_dir
options = build_options({config: File.join(test_files_path(ConfigTest), "config.json"), validate: true})
config = Config.new(options: options)
config.load
config.parse
config.root_dir = "new/dir"
assert_equal "new/dir", config.root_dir
end
end
end
6 changes: 6 additions & 0 deletions test/roku_builder/test_files/keyer_test/manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
title=Test
major_version=1
minor_version=0
build_version=010101.1
mm_icon_focus_hd=pkg:/images/focus.png
mm_icon_focus_sd=pkg:/images/focus.png
Empty file.
4 changes: 2 additions & 2 deletions test/roku_builder/test_inspector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def test_inspector_inspect
to_return(status: 200, body: body, headers: {}))

inspector = Inspector.new(config: @config)
package_info = inspector.inspect(pkg: File.join(test_files_path(InspectorTest), "test.pkg"), password: @password)
package_info = inspector.inspect(pkg: File.join(test_files_path(InspectorTest), "test.pkg"), password: "password")

assert_equal "app_name", package_info[:app_name]
assert_equal "dev_id", package_info[:dev_id]
Expand Down Expand Up @@ -80,7 +80,7 @@ def test_inspector_inspect_old_interface
to_return(status: 200, body: body, headers: {}))

inspector = Inspector.new(config: @config)
package_info = inspector.inspect(pkg: File.join(test_files_path(InspectorTest), "test.pkg"), password: @password)
package_info = inspector.inspect(pkg: File.join(test_files_path(InspectorTest), "test.pkg"), password: "password")

assert_equal "app_name", package_info[:app_name]
assert_equal "dev_id", package_info[:dev_id]
Expand Down
92 changes: 20 additions & 72 deletions test/roku_builder/test_keyer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,116 +5,64 @@
module RokuBuilder
class KeyerTest < Minitest::Test
def setup
options = build_options
@config = Config.new(options: options)
@device_config = {
ip: "111.222.333",
user: "user",
password: "password"
}
@config.instance_variable_set(:@parsed, {root_dir: "/tmp", device_config: @device_config, init_params: {}})
@connection = Minitest::Mock.new
@faraday = Minitest::Mock.new
@response = Minitest::Mock.new

@config = build_config_object(KeyerTest)
@requests = []
end
def teardown
@connection.verify
@faraday.verify
@response.verify
@requests.each {|req| remove_request_stub(req)}
end
def test_keyer_dev_id
path = "/plugin_package"
body = "v class=\"roku-font-5\"><label>Your Dev ID: &nbsp;</label> dev_id<hr></div>"
@requests.push(stub_request(:get, "http://192.168.0.100/plugin_package").
to_return(status: 200, body: body, headers: {}))

@connection.expect(:get, @response, [path])
@response.expect(:body, body)
@response.expect(:body, body)
@faraday.expect(:request, nil, [:digest, @device_config[:user], @device_config[:password]])
@faraday.expect(:adapter, nil, [Faraday.default_adapter])

dev_id = nil
keyer = Keyer.new(config: @config)
Faraday.stub(:new, @connection, @faraday) do
dev_id = keyer.dev_id
end
dev_id = keyer.dev_id

assert_equal "dev_id", dev_id
end
def test_keyer_dev_id_old_interface
path = "/plugin_package"
body = "<p> Your Dev ID: <font face=\"Courier\">dev_id</font> </p>"
@requests.push(stub_request(:get, "http://192.168.0.100/plugin_package").
to_return(status: 200, body: body, headers: {}))

@connection.expect(:get, @response, [path])
@response.expect(:body, body)
@faraday.expect(:request, nil, [:digest, @device_config[:user], @device_config[:password]])
@faraday.expect(:adapter, nil, [Faraday.default_adapter])


dev_id = nil
keyer = Keyer.new(config: @config)
Faraday.stub(:new, @connection, @faraday) do
dev_id = keyer.dev_id
end
dev_id = keyer.dev_id

assert_equal "dev_id", dev_id
end

def test_keyer_rekey_changed
io = Minitest::Mock.new
path = "/plugin_inspect"
password = "password"
payload ={
mysubmit: "Rekey",
password: password,
archive: io
}

@connection.expect(:post, @response) do |arg1, arg2|
assert_equal path, arg1
assert_equal payload[:mysubmit], arg2[:mysubmit]
assert_equal payload[:password], arg2[:passwd]
assert payload[:archive] === arg2[:archive]
end
@faraday.expect(:headers, {})
@faraday.expect(:request, nil, [:digest, @device_config[:user], @device_config[:password]])
@faraday.expect(:request, nil, [:multipart])
@faraday.expect(:request, nil, [:url_encoded])
@faraday.expect(:adapter, nil, [Faraday.default_adapter])

@requests.push(stub_request(:post, "http://192.168.0.100/plugin_inspect").
to_return(status: 200, body: "", headers: {}))
# This test fails with the following seeds due to the random number
# generator spitting out the same number twice
# SEED=21894
# SEED=31813
dev_id = Proc.new {"#{Random.rand(100)}"}
dev_id = Proc.new {"#{Random.rand(999999999999)}"}
keyer = Keyer.new(config: @config)
key_changed = nil
Faraday.stub(:new, @connection, @faraday) do
Faraday::UploadIO.stub(:new, io) do
keyer.stub(:dev_id, dev_id) do
key_changed = keyer.rekey(keyed_pkg: "pkg/path", password: password)
end
end
keyer.stub(:dev_id, dev_id) do
key_changed = keyer.rekey(keyed_pkg: File.join(test_files_path(KeyerTest), "test.pkg"),
password: "password")
end

assert key_changed

io.verify
end

def test_keyer_generate_new_key
@connection.expect(:puts, nil, ["genkey"])
@connection.expect(:waitfor, nil) do |config, &blk|
connection = Minitest::Mock.new()
connection.expect(:puts, nil, ["genkey"])
connection.expect(:waitfor, nil) do |config, &blk|
assert_equal(/./, config['Match'])
assert_equal(false, config['Timeout'])
txt = "Password: password\nDevID: devid\n"
blk.call(txt)
true
end
@connection.expect(:close, nil, [])
connection.expect(:close, nil, [])

keyer = Keyer.new(config: @config)
Net::Telnet.stub(:new, @connection) do
Net::Telnet.stub(:new, connection) do
keyer.send(:generate_new_key)
end
end
Expand Down
10 changes: 10 additions & 0 deletions test/roku_builder/test_tester.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,15 @@ def test_tester_handle_text_partial_text
refute tester.instance_variable_get(:@in_tests)
assert_equal verify_text, tester.instance_variable_get(:@logs)
end

def test_tester_handle_text_used_connection
tester = Tester.new(config: @config)

text = ["connection already in use"]

assert_raises IOError do
tester.send(:handle_text, {txt: text.join("\n")})
end
end
end
end

0 comments on commit fab4721

Please sign in to comment.