Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add command to import adb public key #10

Merged
merged 1 commit into from
Aug 30, 2019
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
5 changes: 5 additions & 0 deletions lib/di.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
require 'stf/interactor/remove_all_user_devices_interactor'
require 'stf/interactor/get_keys_interactor'
require 'stf/interactor/get_values_interactor'
require 'stf/interactor/add_adb_public_key'
require 'stf/validate/uri_validator'

class DI
Expand Down Expand Up @@ -64,6 +65,10 @@ def init (opts = {})
c.register(:uri_validator,
-> {Stf::URIValidator.new},
memoize: true)

c.register(:add_adb_public_key_interactor,
-> {Stf::AddAdbPublicKeyInteractor.new},
memoize: true)
end

def [](what)
Expand Down
5 changes: 5 additions & 0 deletions lib/stf/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ def stop_debug(serial)
return response.success
end

def add_adb_public_key(adb_public_key)
response = execute '/api/v1/user/adbPublicKeys', Net::HTTP::Post, { publickey: adb_public_key }.to_json
return response.success
end

private

def execute(relative_url, type, body='')
Expand Down
20 changes: 20 additions & 0 deletions lib/stf/interactor/add_adb_public_key.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'di'

require 'stf/client'
require 'stf/log/log'

module Stf
class AddAdbPublicKeyInteractor
include Log

def execute(adb_public_key_location)
public_key = File.open(adb_public_key_location, 'rb', &:read)
success = DI[:stf].add_adb_public_key public_key
if success
logger.info "adb public key from '#{adb_public_key_location}' has been added"
elsif logger.error "Can't add public key from '#{adb_public_key_location}'"
return false
end
end
end
end
13 changes: 13 additions & 0 deletions lib/stf/view/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,19 @@ module CLI
c.action {DI[:remove_all_user_devices_interactor].execute}
end

desc 'Add adb public key into STF'
command :trustme do |c|
c.desc 'Location of adb public key'
c.flag [:k, :adb_public_key_location]
c.default_value '~/.android/adbkey.pub'

c.action do |_, options, _|
filename = File.expand_path(options[:adb_public_key_location])
exit_now!("File does not exist: '#{options[:adb_public_key_location]}'") unless File.exist? filename
DI[:add_adb_public_key_interactor].execute(options[:adb_public_key_location])
end
end

exit run(ARGV)
end
end
Expand Down
6 changes: 6 additions & 0 deletions spec/lib/stf_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,10 @@
expect(actual.length).to eq expected
end

it 'should correctly add adb public key' do
result = @stf.add_adb_public_key 'UDKDU15A20001021 foo@foo.foo'

expect(result).to be true
end

end
6 changes: 6 additions & 0 deletions spec/support/fake_stf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ class FakeSTF < Sinatra::Base
json_response 200, 'user/DELETE_devices.json'
end

post '/api/v1/user/adbPublicKeys' do
return 404 unless @env['HTTP_AUTHORIZATION'].eql? "Bearer #{FakeSTF.fake_token}"

json_response 200, 'user/POST_adbPublicKeys.json'
end

private

def json_response(response_code, file_name)
Expand Down
3 changes: 3 additions & 0 deletions spec/support/fixtures/user/POST_adbPublicKeys.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"success": true
}