diff --git a/lib/di.rb b/lib/di.rb index 9a96c0e..100d4c8 100644 --- a/lib/di.rb +++ b/lib/di.rb @@ -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 @@ -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) diff --git a/lib/stf/client.rb b/lib/stf/client.rb index 0a9de63..d5b35b8 100644 --- a/lib/stf/client.rb +++ b/lib/stf/client.rb @@ -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='') diff --git a/lib/stf/interactor/add_adb_public_key.rb b/lib/stf/interactor/add_adb_public_key.rb new file mode 100644 index 0000000..3fc331b --- /dev/null +++ b/lib/stf/interactor/add_adb_public_key.rb @@ -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 \ No newline at end of file diff --git a/lib/stf/view/cli.rb b/lib/stf/view/cli.rb index fc1d4a2..a0156ad 100644 --- a/lib/stf/view/cli.rb +++ b/lib/stf/view/cli.rb @@ -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 diff --git a/spec/lib/stf_spec.rb b/spec/lib/stf_spec.rb index 8252d06..305aa23 100644 --- a/spec/lib/stf_spec.rb +++ b/spec/lib/stf_spec.rb @@ -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 \ No newline at end of file diff --git a/spec/support/fake_stf.rb b/spec/support/fake_stf.rb index 552c022..eb1614b 100644 --- a/spec/support/fake_stf.rb +++ b/spec/support/fake_stf.rb @@ -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) diff --git a/spec/support/fixtures/user/POST_adbPublicKeys.json b/spec/support/fixtures/user/POST_adbPublicKeys.json new file mode 100644 index 0000000..28e7be1 --- /dev/null +++ b/spec/support/fixtures/user/POST_adbPublicKeys.json @@ -0,0 +1,3 @@ +{ + "success": true +} \ No newline at end of file