cloudfs 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,107 @@
1
+ require_relative 'rest_adapter'
2
+
3
+ module CloudFS
4
+ # User class maintains user profile information
5
+ #
6
+ # @author Mrinal Dhillon
7
+ class User
8
+
9
+ # @!attribute [r] id
10
+ # @return [String] internal id of user
11
+ def id
12
+ @properties[:id]
13
+ end
14
+
15
+ # @!attribute [r] username
16
+ # @return [String] end-user's username
17
+ def username
18
+ @properties[:username]
19
+ end
20
+
21
+ # @!attribute [r] first_name
22
+ # @return [String] first name of user
23
+ def first_name
24
+ @properties[:first_name]
25
+ end
26
+
27
+ # @!attribute [r] last_name
28
+ # @return [String] last name of user
29
+ def last_name
30
+ @properties[:last_name]
31
+ end
32
+
33
+ # @!attribute [r] email
34
+ # @return [String] email id of user
35
+ def email
36
+ @properties[:email]
37
+ end
38
+
39
+ # @!attribute [r] created_at
40
+ # @return [Time] account creation time
41
+ def created_at
42
+ if @properties[:created_at]
43
+ Time.at(@properties[:created_at]/1000.0)
44
+ else
45
+ nil
46
+ end
47
+ end
48
+
49
+ # @!attribute [r] last_login
50
+ # @return [Time] last login time
51
+ def last_login
52
+ if @properties[:last_login]
53
+ Time.at(@properties[:last_login]/1000.0)
54
+ else
55
+ nil
56
+ end
57
+ end
58
+
59
+ # @param rest_adapter [RestAdapter] cloudfs RESTful api object
60
+ # @param [Hash] properties metadata of user
61
+ # @option properties [String] :username
62
+ # @option properties [Fixnum] :created_at in milliseconds since epoch
63
+ # @option properties [String] :first_name
64
+ # @option properties [String] :last_name
65
+ # @option properties [String] :email
66
+ # @option properties [Fixnum] :last_login in milliseconds since epoch
67
+ # @option properties [String] :id
68
+ def initialize(rest_adapter, ** properties)
69
+ fail RestAdapter::Errors::ArgumentError,
70
+ "invalid rest_adapter type #{rest_adapter.class}, expected CloudFS::Client" unless rest_adapter.is_a?(CloudFS::RestAdapter)
71
+
72
+ @rest_adapter = rest_adapter
73
+ set_user_info(** properties)
74
+ end
75
+
76
+ # @see #initialize
77
+ # @review required parameters
78
+ def set_user_info(** properties)
79
+ properties.fetch(:username) { fail RestAdapter::Errors::ArgumentError,
80
+ 'Missing required username' }
81
+ properties.fetch(:id) { fail RestAdapter::Errors::ArgumentError,
82
+ 'Missing required id' }
83
+ @properties = properties
84
+ end
85
+
86
+ # Refresh this user's metadata from server
87
+ # @return [User] returns self
88
+ def refresh
89
+ response = @rest_adapter.get_profile
90
+ set_user_info(** response)
91
+ self
92
+ end
93
+
94
+ # @return [String]
95
+ # @!visibility private
96
+ def to_s
97
+ str = "#{self.class}: username: #{@properties[:username]}"
98
+ str << ", first name: #{@properties[:first_name]}" unless RestAdapter::Utils.is_blank?(@properties[:first_name])
99
+ str << ", last name: #{@properties[:last_name]}" unless RestAdapter::Utils.is_blank?(@properties[:last_name])
100
+ str << ", email: #{@properties[:email]}" unless RestAdapter::Utils.is_blank?(@properties[:email])
101
+ str
102
+ end
103
+
104
+ alias inspect to_s
105
+ private :set_user_info
106
+ end
107
+ end
@@ -0,0 +1,3 @@
1
+ module CloudFS
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,93 @@
1
+ require_relative 'spec_helper'
2
+
3
+ describe CloudFS::Account do
4
+ before do
5
+ session = CloudFS::Session.new(Configuration::HOST, Configuration::CLIENT_ID, Configuration::SECRET)
6
+ session.authenticate(Configuration::USERNAME, Configuration::PASSWORD)
7
+ @account_object = session.account
8
+ end
9
+
10
+ describe 'Get Account Id' do
11
+ it '#getid' do
12
+ @account_id = @account_object.id
13
+ @account_id.must_be_instance_of String
14
+ @account_id.length.must_equal 36
15
+ end
16
+ end
17
+
18
+ describe 'Get Storage Usage' do
19
+ it '#getstorageusage' do
20
+ @storage_usage = @account_object.storage_usage
21
+ @storage_usage.wont_be_nil
22
+ @storage_usage.must_be_instance_of Fixnum
23
+ end
24
+ end
25
+
26
+ describe 'Get storage limit' do
27
+ it '#getstoragelimit' do
28
+ @storage_limit = @account_object.storage_limit
29
+ @storage_limit.wont_be_nil
30
+ @storage_limit.must_be_instance_of Fixnum
31
+ end
32
+ end
33
+
34
+
35
+ describe 'Get over storage limit' do
36
+ it '#getoverstoragelimit' do
37
+ @over_storage_limit = @account_object.over_storage_limit
38
+ @over_storage_limit.wont_be_nil
39
+ @over_storage_limit.must_equal false
40
+ end
41
+ end
42
+
43
+ describe 'Get state id' do
44
+ it '#getstateid' do
45
+ @state_id = @account_object.state_id
46
+ @state_id.wont_be_nil
47
+ @state_id.must_be_instance_of String
48
+ @state_id.length.must_equal 5
49
+ end
50
+ end
51
+
52
+ describe 'Get state display name' do
53
+ it '#getstatedisplayname' do
54
+ @state_display_name = @account_object.state_display_name
55
+ @state_display_name.wont_be_nil
56
+ @state_display_name.must_be_instance_of String
57
+ end
58
+ end
59
+
60
+ describe 'Get plan display name' do
61
+ it '#getplandisplayname' do
62
+ @plan_display_name = @account_object.plan_display_name
63
+ @plan_display_name.wont_be_nil
64
+ @plan_display_name.must_be_instance_of String
65
+ end
66
+ end
67
+
68
+ describe 'Get plan id' do
69
+ it '#getplanid' do
70
+ @plan_id = @account_object.plan_id
71
+ @plan_id.wont_be_nil
72
+ @plan_id.must_be_instance_of String
73
+ end
74
+ end
75
+
76
+ describe 'Get session locale' do
77
+ it '#getsessionlocale' do
78
+ @session_locale = @account_object.session_locale
79
+ @session_locale.wont_be_nil
80
+ @session_locale.must_be_instance_of String
81
+ end
82
+ end
83
+
84
+ describe 'Get account locale' do
85
+ it '#getaccountlocale' do
86
+ @account_locale = @account_object.account_locale
87
+ @account_locale.wont_be_nil
88
+ @account_locale.must_be_instance_of String
89
+ end
90
+ end
91
+
92
+
93
+ end
@@ -0,0 +1,37 @@
1
+ require_relative 'spec_helper'
2
+
3
+ describe CloudFS::Container do
4
+ before do
5
+ session = CloudFS::Session.new(Configuration::HOST, Configuration::CLIENT_ID, Configuration::SECRET)
6
+ session.authenticate(Configuration::USERNAME, Configuration::PASSWORD)
7
+ @subject = session.filesystem
8
+ @test_folder = @subject.root.create_folder(Configuration::TEST_FOLDER, exists: 'OVERWRITE')
9
+ end
10
+
11
+ describe 'Container Initialize' do
12
+ before do
13
+ @new_container = @test_folder.create_folder('container_test', exists: 'OVERWRITE')
14
+ end
15
+ it '#intialize' do
16
+ @new_container.type.must_equal 'folder'
17
+ @new_container.name.must_equal 'container_test'
18
+ end
19
+ after do
20
+ @new_container.delete
21
+ end
22
+ end
23
+
24
+ describe 'List Container' do
25
+ before do
26
+ @new_container = @test_folder.create_folder('container_test', exists: 'OVERWRITE')
27
+ end
28
+ it '#list' do
29
+ items = @test_folder.list
30
+ items.must_be_instance_of Array
31
+ end
32
+ after do
33
+ @new_container.delete
34
+ end
35
+ end
36
+
37
+ end
@@ -0,0 +1,134 @@
1
+ require_relative 'spec_helper'
2
+
3
+ describe CloudFS::File do
4
+ before do
5
+ session = CloudFS::Session.new(Configuration::HOST, Configuration::CLIENT_ID, Configuration::SECRET)
6
+ session.authenticate(Configuration::USERNAME, Configuration::PASSWORD)
7
+ @subject = session.filesystem
8
+ @test_folder = @subject.root.create_folder(Configuration::TEST_FOLDER, exists: 'OVERWRITE')
9
+ @file_folder =@test_folder.create_folder('file_test', exists: 'OVERWRITE')
10
+ @file = @file_folder.upload('file test', name: 'file_test.txt', exists: 'OVERWRITE', upload_io: true)
11
+ end
12
+
13
+ it 'Should have a file' do
14
+ @file.type.must_equal 'file'
15
+ @file.name.must_equal 'file_test.txt'
16
+ end
17
+
18
+ describe 'get mime' do
19
+ it '#mime' do
20
+ @filetype = @file.mime
21
+ @filetype.must_equal 'text/plain; charset=utf-8'
22
+ end
23
+ end
24
+
25
+ describe 'get extension' do
26
+ it '#extension' do
27
+ @filetype = @file.extension
28
+ @filetype.must_equal 'txt'
29
+ end
30
+ end
31
+
32
+ describe 'get size' do
33
+ it '#size' do
34
+ @filesize = @file.size
35
+ @filesize.must_equal 9
36
+ end
37
+ end
38
+
39
+ describe 'file read' do
40
+ it '#read' do
41
+ @read_full_data = @file.read
42
+ @file.rewind
43
+ @read_partial_data = @file.read(bytecount: 4)
44
+ @read_full_data.must_equal 'file test'
45
+ @read_partial_data.must_equal 'file'
46
+ end
47
+ after do
48
+ @file.rewind
49
+ end
50
+ end
51
+
52
+ describe 'file tell' do
53
+ it '#tell' do
54
+ @offset_before_read = @file.tell
55
+ @file.read
56
+ @offset_after_read = @file.tell
57
+ @offset_before_read.must_equal 0
58
+ @offset_after_read.must_equal 9
59
+ end
60
+ after do
61
+ @file.rewind
62
+ end
63
+ end
64
+
65
+ describe 'file rewind' do
66
+ it '#rewind' do
67
+ @offset_before_read = @file.tell
68
+ @file.read
69
+ @offset_after_read = @file.tell
70
+ @file.rewind
71
+ @offset_after_rewind = @file.tell
72
+ @offset_before_read.must_equal 0
73
+ @offset_after_read.must_equal 9
74
+ @offset_after_rewind.must_equal 0
75
+ end
76
+ after do
77
+ @file.rewind
78
+ end
79
+ end
80
+
81
+ describe 'file download' do
82
+ before do
83
+ logged_in_user = ENV['USER']
84
+ @file_path = '/home/' + logged_in_user + '/ruby-file-download'
85
+
86
+ @local_path_exist = File.directory?(@file_path)
87
+
88
+ if @local_path_exist == false
89
+ Dir.mkdir(@file_path)
90
+ end
91
+
92
+ @file_exist_before_download = File.exist?(@file_path + '/file_test.txt')
93
+ end
94
+ it '#download' do
95
+ file_size = 0
96
+ downloaded_size = 0
97
+ @file.download(@file_path) do |size, downloaded|
98
+ file_size = size
99
+ downloaded_size = downloaded
100
+ end
101
+
102
+ downloaded_size.must_equal file_size
103
+ @file_exist_after_download = File.exist?(@file_path + '/file_test.txt')
104
+ @file_exist_before_download.must_equal false
105
+ @file_exist_after_download.must_equal true
106
+
107
+ end
108
+ after do
109
+ if @file_exist_after_download == true
110
+ File.delete(@file_path + '/file_test.txt')
111
+ Dir.delete(@file_path)
112
+ end
113
+ end
114
+ end
115
+
116
+ describe 'file download URL' do
117
+ it '#should return download url' do
118
+ url = @file.download_url
119
+ puts (url)
120
+
121
+ url.wont_be_nil
122
+ url.wont_be_empty
123
+ end
124
+ end
125
+
126
+ describe 'list file versions' do
127
+ it '#listfileversions' do
128
+ @file_versions = @file.versions
129
+ @file_versions.wont_be_nil
130
+ @file_versions.must_be_instance_of Array
131
+ end
132
+ end
133
+
134
+ end
@@ -0,0 +1,16 @@
1
+ require_relative 'spec_helper'
2
+
3
+ describe CloudFS::FileSystem do
4
+ before do
5
+ session = CloudFS::Session.new(Configuration::HOST, Configuration::CLIENT_ID, Configuration::SECRET)
6
+ session.authenticate(Configuration::USERNAME, Configuration::PASSWORD)
7
+ @subject = session.filesystem
8
+ @test_folder = @subject.root.create_folder(Configuration::TEST_FOLDER, exists: 'OVERWRITE')
9
+ end
10
+
11
+ it 'Should have a root' do
12
+ @subject.root.type.must_equal 'folder'
13
+ @subject.root.name.must_equal 'root'
14
+ end
15
+
16
+ end
@@ -0,0 +1,106 @@
1
+ require_relative 'spec_helper'
2
+
3
+ describe CloudFS::Folder do
4
+ before do
5
+ session = CloudFS::Session.new(Configuration::HOST, Configuration::CLIENT_ID, Configuration::SECRET)
6
+ session.authenticate(Configuration::USERNAME, Configuration::PASSWORD)
7
+ @subject = session.filesystem
8
+ @test_folder = @subject.root.create_folder(Configuration::TEST_FOLDER, exists: 'OVERWRITE')
9
+ end
10
+
11
+ describe 'Creating a Folder' do
12
+
13
+ it '#create_folder' do
14
+ @create_folder = @test_folder.create_folder('folder_test')
15
+ @create_folder.type.must_equal 'folder'
16
+
17
+ @create_folder_rename = @test_folder.create_folder('folder_test', exists: 'RENAME')
18
+ @create_folder_rename.type.must_equal 'folder'
19
+ @create_folder_rename.name.must_equal 'folder_test (1)'
20
+ end
21
+
22
+ after do
23
+ @create_folder.delete
24
+ @create_folder_rename.delete
25
+ end
26
+ end
27
+
28
+ describe 'Uploading a File Via IO' do
29
+
30
+ before do
31
+ @create_folder = @test_folder.create_folder('folder_test')
32
+ end
33
+
34
+ it '#upload' do
35
+ @file = @create_folder.upload('file test', name:'file_test.txt', upload_io: true)
36
+ @file.type.must_equal 'file'
37
+ @file.name.must_equal 'file_test.txt'
38
+ end
39
+
40
+ after do
41
+ @create_folder.delete
42
+ end
43
+ end
44
+
45
+ describe 'Uploading Local File' do
46
+
47
+ before do
48
+ @create_folder = @test_folder.create_folder('folder_test')
49
+ logged_in_user = ENV['USER']
50
+ @directory_path = '/home/' + logged_in_user + '/file_upload'
51
+ @local_path_exist = File.directory?(@directory_path)
52
+
53
+ if @local_path_exist == false
54
+ Dir.mkdir(@directory_path)
55
+ end
56
+
57
+ @file_path = @directory_path + '/file_test.txt'
58
+ File.open(@file_path, 'w') {|f| f.write('test content') }
59
+ end
60
+
61
+ it '#upload' do
62
+ @file = @create_folder.upload(@file_path)
63
+ @file.type.must_equal 'file'
64
+ @file.name.must_equal 'file_test.txt'
65
+ end
66
+
67
+ after do
68
+ @create_folder.delete
69
+ File.delete(@file_path)
70
+ Dir.delete(@directory_path)
71
+ end
72
+ end
73
+
74
+ describe 'Listing the folder content, traversing ' do
75
+ before do
76
+ @root_folder = @test_folder.create_folder('root_folder', exists: 'OVERWRITE')
77
+ @folder_child_01 = @root_folder.create_folder('folder_child_01', exists: 'OVERWRITE')
78
+ @folder_child_01.upload('folder child 01 content', name: 'folder_child_01.txt', upload_io: true)
79
+ end
80
+
81
+ it 'Should show the child folders and files' do
82
+ root_folder_content = @root_folder.list
83
+ root_folder_content.length.must_equal 1
84
+ root_folder_content.first.name.must_equal 'folder_child_01'
85
+
86
+ child_folder_content = root_folder_content.first.list
87
+ child_folder_content.length.must_equal 1
88
+ child_folder_content.first.name.must_equal 'folder_child_01.txt'
89
+
90
+ @root_folder.delete(commit: false, force: true)
91
+
92
+ root_trash_folder = @root_folder.list
93
+ root_trash_folder.length.must_equal 1
94
+ root_trash_folder.first.name.must_equal 'folder_child_01'
95
+
96
+ first_trash_child = root_trash_folder.first.list
97
+ first_trash_child.length.must_equal 1
98
+ first_trash_child.first.name.must_equal 'folder_child_01.txt'
99
+ end
100
+
101
+ after do
102
+ @root_folder.delete(commit: true, force: true)
103
+ end
104
+ end
105
+
106
+ end