Class: GoogleDriveEasyTest

Inherits:
Test::Unit::TestCase
  • Object
show all
Defined in:
lib/googledrive-easy-test.rb

Instance Method Summary collapse

Instance Method Details

#configure_env_fileObject



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/googledrive-easy-test.rb', line 21

def configure_env_file
  # Write temp json file to load from for test
  @env_file = Tempfile.new("api_var_file")
  @env_file.write(JSON.generate({
                                  "CLIENT_ID": @client_id,
                                  "REFRESH_TOKEN": @refresh_token,
                                  "CLIENT_SECRET": @client_secret,
                                }))
  @env_file.flush
  return @env_file
end

#configure_environmentObject



8
9
10
11
12
# File 'lib/googledrive-easy-test.rb', line 8

def configure_environment
  ENV['DRIVEAPI_CLIENT_ID'] = @client_id
  ENV['DRIVEAPI_CLIENT_SECRET'] = @client_secret
  ENV['DRIVEAPI_REFRESH_TOKEN'] = @refresh_token
end

#deconfigure_env_fileObject



33
34
35
# File 'lib/googledrive-easy-test.rb', line 33

def deconfigure_env_file
  @env_file.delete
end

#deconfigure_environmentObject



14
15
16
17
18
19
# File 'lib/googledrive-easy-test.rb', line 14

def deconfigure_environment
  vars = %w(DRIVEAPI_CLIENT_ID DRIVEAPI_CLIENT_SECRET DRIVEAPI_REFRESH_TOKEN)
  vars.each do |v|
    ENV.delete(v)
  end
end

#setupObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/googledrive-easy-test.rb', line 37

def setup
  # Unset env variables before each test to keep things predictable
  deconfigure_environment

  # Used to omit tests that require real drive connectivity.
  @test_env_vars_defined = (ENV['TEST_CLIENT_ID'] && ENV['TEST_CLIENT_SECRET'] && ENV['TEST_CLIENT_SECRET'])

  # Init
  @client_id = ENV['TEST_CLIENT_ID'] || 'test-id'
  @client_secret = ENV['TEST_CLIENT_SECRET'] || 'test-secret'
  @refresh_token = ENV['TEST_REFRESH_TOKEN'] || 'test-token'
  @access_token = nil

  @drive = GoogleDrive::new
  @drive.instance_variable_set(:@api_key_file, "/tmp/test_drive_api_keys.json")
end

#test_create_service_environmentObject



202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/googledrive-easy-test.rb', line 202

def test_create_service_environment
  omit("Test Environment not correctly configured for live tests") unless @test_env_vars_defined
  configure_environment

  2.times do
    # Authorize
    result = @drive.create_service(mode: 'environment')
    assert(result, "Authorization from configured environment should succeed.")
    assert(@drive.instance_variable_get(:@credentials), "Credentials should be set")
    puts @drive.instance_variable_get(:@credentials)
    assert_instance_of(Google::Apis::DriveV3::DriveService, result, "Should return instance of Google Drive Service")
    @drive.set_raise_error
  end
end

#test_create_service_environment_fileObject

Auth tests (Require real credentials) #



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/googledrive-easy-test.rb', line 184

def test_create_service_environment_file
  omit("Test Environment not correctly configured for live tests") unless @test_env_vars_defined
  configure_env_file

  # Load environment
  # Use this, or just set the path directly.
  @drive.load_api_keys_from_file(path:@env_file.path)

  2.times do
    # Authorize
    result = @drive.create_service(mode: 'manual')
    assert(result, "Authorization from configured environment should succeed.")
    assert(@drive.instance_variable_get(:@credentials), "Credentials should be set")
    assert_instance_of(Google::Apis::DriveV3::DriveService, result, "Should return instance of Google Drive Service")
    @drive.set_raise_error
  end
end

#test_create_service_manualObject



217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/googledrive-easy-test.rb', line 217

def test_create_service_manual
  omit("Test Environment not correctly configured for live tests") unless @test_env_vars_defined
  @drive.set_api_keys(client_id: @client_id, client_secret: @client_secret, refresh_token: @refresh_token)

  2.times do
    # Authorize
    result = @drive.create_service(mode: 'manual')
    assert(result, "Authorization from manual configuration should succeed.")
    assert(@drive.instance_variable_get(:@credentials), "Credentials should be set")
    assert_instance_of(Google::Apis::DriveV3::DriveService, result, "Should return instance of Google Drive Service")
    @drive.set_raise_error
  end
end

#test_find_directory_idObject



288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/googledrive-easy-test.rb', line 288

def test_find_directory_id
  omit("Test Environment not correctly configured for live tests") unless @test_env_vars_defined
  @drive.set_api_keys(client_id: @client_id, client_secret: @client_secret, refresh_token: @refresh_token)
  @drive.create_service(mode:'manual')

  2.times do
    res = @drive.find_directory_id("rxg")
    assert_equal("10l2ahCetnm1_147rRcuaObTwrntqQuRz", res)
    # assert_instance_of(Google::Apis::DriveV3::File, res, 'Should be instance of Google::Apis::DriveV3::File')
    res = @drive.get_file_info("10l2ahCetnm1_147rRcuaObTwrntqQuRz")
    @drive.set_raise_error
  end
end

#test_find_directory_id_nodirObject



302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/googledrive-easy-test.rb', line 302

def test_find_directory_id_nodir
  omit("Test Environment not correctly configured for live tests") unless @test_env_vars_defined
  @drive.set_api_keys(client_id: @client_id, client_secret: @client_secret, refresh_token: @refresh_token)
  @drive.create_service(mode:'manual')

  res = @drive.find_directory_id("nonexistantdirectory")
  assert_false(res, "Result should be false")

  @drive.set_raise_error
  assert_raise(RuntimeError) { res = @drive.find_directory_id("nonexistantdirectory") }
  # Doesn't raise on not-found
end

#test_generate_api_secret_hashObject

Object generation tests #



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/googledrive-easy-test.rb', line 126

def test_generate_api_secret_hash
  @drive.instance_variable_set(:@client_id, @client_id)
  @drive.instance_variable_set(:@client_secret, @client_secret)

  2.times do
    api_hash = @drive.generate_api_secret_hash
    assert_equal(@client_id, api_hash['installed']['client_id'], 'Hash should contain client id')
    assert_equal(@client_secret, api_hash['installed']['client_secret'], 'Hash should contain client secret')
    @drive.set_raise_error
  end
end

#test_generate_temp_token_fileObject



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/googledrive-easy-test.rb', line 164

def test_generate_temp_token_file
  # Partially Configure object
  @drive.instance_variable_set(:@client_id, @client_id)
  @drive.instance_variable_set(:@refresh_token, @refresh_token)
  token_file_user = @drive.instance_variable_get(:@token_file_user)

  2.times do
    tmp_file_path = @drive.generate_temp_token_file
    tmp_file_contents = File.read(tmp_file_path)
    assert(tmp_file_contents.include?(token_file_user), "Result should contain token_file_user")
    assert(tmp_file_contents.include?(@client_id), "Result should contain client_id")
    assert(tmp_file_contents.include?(@refresh_token), "Result should contain refresh_token")
    @drive.set_raise_error
  end
end

#test_generate_token_yaml_failObject



154
155
156
157
158
159
160
161
162
# File 'lib/googledrive-easy-test.rb', line 154

def test_generate_token_yaml_fail
  # Partially Configure object
  # @drive_wrapper.instance_variable_set(:@client_id, @client_id)
  @drive.instance_variable_set(:@refresh_token, @refresh_token)
  assert(!@drive.generate_token_yaml, "Generation of token yaml should fail with partial config")

  @drive.set_raise_error
  assert_raise(RuntimeError) { @drive.generate_token_yaml }
end

#test_generate_token_yaml_successObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/googledrive-easy-test.rb', line 138

def test_generate_token_yaml_success
  # TODO: Test feels partial/fragile. Improve?
  # Partially Configure object
  @drive.instance_variable_set(:@client_id, @client_id)
  @drive.instance_variable_set(:@refresh_token, @refresh_token)
  token_file_user = @drive.instance_variable_get(:@token_file_user)

  2.times do
    yaml_string = @drive.generate_token_yaml
    assert(yaml_string.include?(token_file_user), "Result should contain token_file_user")
    assert(yaml_string.include?(@client_id), "Result should contain client_id")
    assert(yaml_string.include?(@refresh_token), "Result should contain refresh_token")
    @drive.set_raise_error
  end
end

#test_get_all_filesObject

No great way to unit test the following: get_oauth_credentials_via_loopback get_oauth_credentials_via_input find_directory_id download_file upload_file list_files



249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/googledrive-easy-test.rb', line 249

def test_get_all_files
  omit("Test Environment not correctly configured for live tests") unless @test_env_vars_defined
  # configure_environment

  @drive.set_api_keys(client_id: @client_id, client_secret: @client_secret, refresh_token: @refresh_token)
  @drive.create_service(mode:'manual')

  2.times do
    res = @drive.get_all_files(name: "rxgos*")
    assert_instance_of(Array, res, "Returns list")
    @drive.set_raise_error
  end
end

#test_get_file_infoObject



263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/googledrive-easy-test.rb', line 263

def test_get_file_info
  omit("Test Environment not correctly configured for live tests") unless @test_env_vars_defined
  @drive.set_api_keys(client_id: @client_id, client_secret: @client_secret, refresh_token: @refresh_token)
  @drive.create_service(mode:'manual')

  2.times do
    res = @drive.get_file_info("10l2ahCetnm1_147rRcuaObTwrntqQuRz")
    assert_instance_of(Hash, res, 'Should return a Hash')
    assert_equal("rxg", res[:name], "Name should be 'rxg'")
    # assert_instance_of(Google::Apis::DriveV3::File, res, 'Should be instance of Google::Apis::DriveV3::File')
    @drive.set_raise_error
  end
end

#test_get_file_info_nofileObject



277
278
279
280
281
282
283
284
285
286
# File 'lib/googledrive-easy-test.rb', line 277

def test_get_file_info_nofile
  omit("Test Environment not correctly configured for live tests") unless @test_env_vars_defined
  @drive.set_api_keys(client_id: @client_id, client_secret: @client_secret, refresh_token: @refresh_token)
  @drive.create_service(mode:'manual')

  res = @drive.get_file_info("10l2ahCetnm1_147rRcuaObTwrntqQZZZ")
  assert_false(res, "Result should be false")

  # Doesn't raise on not-found
end

#test_loading_api_vars_from_env_missing_failObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/googledrive-easy-test.rb', line 72

def test_loading_api_vars_from_env_missing_fail
  ENV['DRIVEAPI_CLIENT_ID'] = @client_id
  ENV.delete('DRIVEAPI_CLIENT_SECRET')
  ENV['DRIVEAPI_REFRESH_TOKEN'] = @refresh_token

  result = @drive.load_api_keys_from_env
  assert(!result, "load_api_keys_from_env should return false")
  assert_equal(nil, @drive.instance_variable_get(:@client_id), "Client ID should not be loaded")
  assert_equal(nil, @drive.instance_variable_get(:@client_secret), "Client Secret should not be loaded")
  assert_equal(nil, @drive.instance_variable_get(:@refresh_token), "Refresh Token should not be loaded")

  @drive.set_raise_error
  exception = assert_raise(RuntimeError) { @drive.load_api_keys_from_env }
  assert_equal("DRIVEAPI_CLIENT_SECRET export variable not set.", exception.message)
end

#test_loading_api_vars_from_env_successObject

Loading tests #



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/googledrive-easy-test.rb', line 57

def test_loading_api_vars_from_env_success
  configure_environment

  2.times do |i|
    result = @drive.load_api_keys_from_env
    assert(result, "load_api_keys_from_env should return true")
    assert_equal(@client_id, @drive.instance_variable_get(:@client_id), "Client ID should be loaded")
    assert_equal(@client_secret, @drive.instance_variable_get(:@client_secret), "Client Secret should be loaded")
    assert_equal(@refresh_token, @drive.instance_variable_get(:@refresh_token), "Refresh Token should be loaded")

    # Enable raise error for second run.
    @drive.set_raise_error
  end
end

#test_loading_api_vars_from_file_not_foundObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/googledrive-easy-test.rb', line 106

def test_loading_api_vars_from_file_not_found
  # Override default key file
  @drive.instance_variable_set(:@api_key_file, '/tmp/bogons')

  # Attempt load
  result = @drive.load_api_keys_from_file
  assert(!result, "load_api_keys_from_file should return false")
  assert_equal(nil, @drive.instance_variable_get(:@client_id), "Client ID should be loaded")
  assert_equal(nil, @drive.instance_variable_get(:@client_secret), "Client Secret should be loaded")
  assert_equal(nil, @drive.instance_variable_get(:@refresh_token), "Refresh Token should be loaded")

  @drive.set_raise_error
  exception = assert_raise(RuntimeError) { @drive.load_api_keys_from_file }
  assert_equal("Cannot find /tmp/bogons", exception.message)
end

#test_loading_api_vars_from_file_successObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/googledrive-easy-test.rb', line 88

def test_loading_api_vars_from_file_success
  # Write temp json file to load from for test
  configure_env_file

  # Override default key file
  # @drive.instance_variable_set(:@api_key_file, @env_file.path)
  2.times do |i|
    # Attempt load
    result = @drive.load_api_keys_from_file(path:@env_file.path)
    assert(result, "load_api_keys_from_file should return true")
    assert_equal(@client_id, @drive.instance_variable_get(:@client_id), "Client ID should be loaded")
    assert_equal(@client_secret, @drive.instance_variable_get(:@client_secret), "Client Secret should be loaded")
    assert_equal(@refresh_token, @drive.instance_variable_get(:@refresh_token), "Refresh Token should be loaded")
    # Enable raise error for second run.
    @drive.set_raise_error
  end
end