kloudless 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,49 @@
1
+ require_relative "../test_helper"
2
+
3
+ class Kloudless::CollectionTest < Minitest::Test
4
+ Model = Struct.new(:id)
5
+
6
+ def setup
7
+ @raw = <<-JSON
8
+ {
9
+ "total": 2,
10
+ "count": 2,
11
+ "page": 1,
12
+ "objects": [
13
+ {
14
+ "id": 1
15
+ },
16
+ {
17
+ "id": 2
18
+ }
19
+ ]
20
+ }
21
+ JSON
22
+ @json = JSON.parse(@raw)
23
+ @collection = Kloudless::Collection.new(Model, @json)
24
+ end
25
+
26
+ def test_total
27
+ assert_equal 2, @collection.total
28
+ end
29
+
30
+ def test_count
31
+ assert_equal 2, @collection.count
32
+ end
33
+
34
+ def test_page
35
+ assert_equal 1, @collection.page
36
+ end
37
+
38
+ def test_objects
39
+ assert_equal 2, @collection.objects.size
40
+ assert_equal 1, @collection.objects[0]["id"]
41
+ assert_equal 2, @collection.objects[1]["id"]
42
+ end
43
+
44
+ def test_each
45
+ @collection.each do |record|
46
+ assert_kind_of Model, record
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,23 @@
1
+ require_relative "../test_helper"
2
+
3
+ class Kloudless::ErrorTest < Minitest::Test
4
+ Response = Struct.new(:code, :body)
5
+
6
+ def test_from_json
7
+ json = {
8
+ "error_code" => "naming_conflict",
9
+ "message" => "boom town",
10
+ "id" => "request-id",
11
+ "status_code" => "409",
12
+ "conflicting_resource_id" => "conflicting-resource-id"
13
+ }
14
+
15
+ error = Kloudless::Error.from_json(json)
16
+ assert_kind_of Kloudless::NamingConflictError, error
17
+ assert_equal "409", error.status_code
18
+ assert_equal "naming_conflict", error.error_code
19
+ assert_equal "request-id", error.id
20
+ assert_equal "conflicting-resource-id", error.conflicting_resource_id
21
+ assert_equal "boom town", error.message
22
+ end
23
+ end
@@ -0,0 +1,20 @@
1
+ require_relative "../test_helper"
2
+
3
+ class Kloudless::EventTest < Minitest::Test
4
+ def test_list_events
5
+ response = {"objects" => [{"id" => 1, "cursor" => 330}]}
6
+ Kloudless.http.expect(:get, returns: response, args: ["/accounts/1/events", params: {}]) do
7
+ events = Kloudless::Event.list(account_id: 1)
8
+ assert_kind_of Kloudless::Collection, events
9
+ assert_kind_of Kloudless::Event, events.first
10
+ end
11
+ end
12
+
13
+ def test_cursor_events
14
+ response = {"cursor" => 330}
15
+ Kloudless.http.expect(:get, returns: response, args: ["/accounts/1/events/latest"]) do
16
+ event = Kloudless::Event.cursor(account_id: 1)
17
+ assert_kind_of Kloudless::Event, event
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,63 @@
1
+ require_relative "../test_helper"
2
+
3
+ class Kloudless::FileTest < Minitest::Test
4
+ def test_upload_file
5
+ Kloudless.http.expect(:post, returns: {"id" => "foo"}, args: ["/accounts/1/files", params: {}]) do
6
+ file = Kloudless::File.upload(account_id: 1)
7
+ assert_kind_of Kloudless::File, file
8
+ end
9
+ end
10
+
11
+ def test_retrieve_file_metadata
12
+ Kloudless.http.expect(:get, returns: {"id" => "foo"}, args:["/accounts/1/files/2"]) do
13
+ metadata = Kloudless::File.metadata(account_id: 1, file_id: 2)
14
+ assert_kind_of Kloudless::File, metadata # TODO: not really a type
15
+ end
16
+ end
17
+
18
+ def test_rename_file
19
+ Kloudless.http.expect(:patch, returns: {"id" => "foo"}, args:["/accounts/1/files/2", params: {name: "new-name.txt"}]) do
20
+ file = Kloudless::File.rename(account_id: 1, file_id: 2, name: "new-name.txt")
21
+ assert_kind_of Kloudless::File, file
22
+ end
23
+ end
24
+
25
+ def test_download
26
+ Kloudless.http.expect(:get_raw, returns: "FILE CONTENTS", args:["/accounts/1/files/2/contents"]) do
27
+ contents = Kloudless::File.download(account_id: 1, file_id: 2)
28
+ assert_equal "FILE CONTENTS", contents
29
+ end
30
+ end
31
+
32
+ def test_copy
33
+ Kloudless.http.expect(:post, returns: {"id" => "foo"}, args:["/accounts/1/files/2/copy", params: {parent_id: "parent-id"}]) do
34
+ file = Kloudless::File.copy(account_id: 1, file_id: 2, parent_id: "parent-id")
35
+ assert_kind_of Kloudless::File, file
36
+ end
37
+ end
38
+
39
+ def test_delete
40
+ Kloudless.http.expect(:delete, returns: {"id" => "foo"}, args:["/accounts/1/files/2", params: {permanent: true}]) do
41
+ file = Kloudless::File.delete(account_id: 1, file_id: 2, permanent: true)
42
+ assert_kind_of Kloudless::File, file
43
+ end
44
+ end
45
+
46
+ def test_recent
47
+ response = {"objects" => [{"id" => "foo"}]}
48
+ Kloudless.http.expect(:get, returns: response, args:["/accounts/1,2/recent", params: {}]) do
49
+ files = Kloudless::File.recent(account_ids: [1,2])
50
+ assert_kind_of Kloudless::Collection, files
51
+ assert_kind_of Kloudless::File, files.first
52
+ end
53
+ end
54
+
55
+ def test_search
56
+ response = {"objects" => [{"id" => "foo"}]}
57
+ Kloudless.http.expect(:get, returns: response, args:["/accounts/1,2/search", params: {dropbox: "foo"}]) do
58
+ files = Kloudless::File.search(account_ids: [1,2], dropbox: "foo")
59
+ assert_kind_of Kloudless::Collection, files
60
+ assert_kind_of Kloudless::File, files.first
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,47 @@
1
+ require_relative "../test_helper"
2
+
3
+ class Kloudless::FolderTest < Minitest::Test
4
+ def test_create
5
+ Kloudless.http.expect(:post, args: ["/accounts/1/folders", params: {}]) do
6
+ folder = Kloudless::Folder.create(account_id: 1)
7
+ assert_kind_of Kloudless::Folder, folder
8
+ end
9
+ end
10
+
11
+ def test_metadata
12
+ Kloudless.http.expect(:get, args: ["/accounts/1/folders/2"]) do
13
+ folder = Kloudless::Folder.metadata(account_id: 1, folder_id: 2)
14
+ assert_kind_of Kloudless::Folder, folder
15
+ end
16
+ end
17
+
18
+ def test_retrieve
19
+ response = {"objects" => [{"id" => "1"}]}
20
+ Kloudless.http.expect(:get, returns: response, args: ["/accounts/1/folders/2/contents", params: {page_number: 1, page_size: 1}]) do
21
+ folders = Kloudless::Folder.retrieve(account_id: 1, folder_id: 2, page_number: 1, page_size: 1)
22
+ assert_kind_of Kloudless::Collection, folders
23
+ assert_kind_of Kloudless::Folder, folders.first
24
+ end
25
+ end
26
+
27
+ def test_rename
28
+ Kloudless.http.expect(:patch, args: ["/accounts/1/folders/2", params: {name: "foo.md"}]) do
29
+ folder = Kloudless::Folder.rename(account_id: 1, folder_id: 2, name: "foo.md")
30
+ assert_kind_of Kloudless::Folder, folder
31
+ end
32
+ end
33
+
34
+ def test_copy
35
+ Kloudless.http.expect(:post, args: ["/accounts/1/folders/2/copy", params: {parent_id: "parent-id"}]) do
36
+ folder = Kloudless::Folder.copy(account_id: 1, folder_id: 2, parent_id: "parent-id")
37
+ assert_kind_of Kloudless::Folder, folder
38
+ end
39
+ end
40
+
41
+ def test_delete
42
+ Kloudless.http.expect(:delete, args: ["/accounts/1/folders/2", params: {recursive: true, permanent: true}]) do
43
+ folder = Kloudless::Folder.delete(account_id: 1, folder_id: 2, recursive: true, permanent: true)
44
+ assert_kind_of Kloudless::Folder, folder
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,58 @@
1
+ require_relative "../test_helper"
2
+
3
+ class Kloudless::HTTPTest < Minitest::Test
4
+ def http
5
+ Kloudless::HTTP
6
+ end
7
+
8
+ def last_request
9
+ Kloudless::HTTP.last_request
10
+ end
11
+
12
+ def test_get
13
+ http.mock_response(Struct.new(:body).new('{"foo": "bar"}')) do
14
+ json = http.get("/foo")
15
+
16
+ assert_equal URI.parse("https://api.kloudless.com/v0/foo"), last_request.uri
17
+ assert_kind_of Net::HTTP::Get, last_request
18
+
19
+ expected = {"foo" => "bar"}
20
+ assert_equal expected, json
21
+ end
22
+ end
23
+
24
+ def test_delete
25
+ http.mock_response(Struct.new(:body).new('{}')) do
26
+ json = http.delete("/foo/1")
27
+
28
+ assert_equal URI.parse("https://api.kloudless.com/v0/foo/1"), last_request.uri
29
+ assert_kind_of Net::HTTP::Delete, last_request
30
+ end
31
+ end
32
+
33
+ def test_params
34
+ http.mock_response(Struct.new(:body).new('{"foo": "bar"}')) do
35
+ http.get("/foo", params: {page: 2})
36
+ assert_equal URI.parse("https://api.kloudless.com/v0/foo?page=2"), last_request.uri
37
+ end
38
+ end
39
+
40
+ def test_headers
41
+ http.mock_response(Struct.new(:body).new('{"foo": "bar"}')) do
42
+ http.post("/foo", headers: {"X-CUSTOM" => "header-value"})
43
+ assert_equal "header-value", last_request["X-CUSTOM"]
44
+ assert_equal "application/json", last_request["Content-Type"]
45
+
46
+ http.patch("/foo")
47
+ assert_equal "application/json", last_request["Content-Type"]
48
+ end
49
+ end
50
+
51
+ def test_error
52
+ http.mock_response(Struct.new(:body).new('{"error_code": "bad_request"}')) do
53
+ assert_raises Kloudless::BadRequestError do
54
+ http.get("/foo")
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,40 @@
1
+ require_relative "../test_helper"
2
+
3
+ class Kloudless::LinkTest < Minitest::Test
4
+ def test_list_links
5
+ response = {"objects" => [{"id" => 1}]}
6
+ Kloudless.http.expect(:get, returns: response, args: ["/accounts/1,2/links", params: {active: false}]) do
7
+ links = Kloudless::Link.list(account_ids: [1,2], active: false)
8
+ assert_kind_of Kloudless::Collection, links
9
+ assert_kind_of Kloudless::Link, links.first
10
+ end
11
+ end
12
+
13
+ def test_create_link
14
+ Kloudless.http.expect(:post, args: ["/accounts/1/links", params: {file_id: "file-id"}]) do
15
+ link = Kloudless::Link.create(account_id: 1, file_id: "file-id")
16
+ assert_kind_of Kloudless::Link, link
17
+ end
18
+ end
19
+
20
+ def test_retrieve_link
21
+ Kloudless.http.expect(:get, args: ["/accounts/1/links/2", params: {active: true}]) do
22
+ link = Kloudless::Link.retrieve(account_id: 1, link_id: 2, active: true)
23
+ assert_kind_of Kloudless::Link, link
24
+ end
25
+ end
26
+
27
+ def test_update_link
28
+ Kloudless.http.expect(:patch, args: ["/accounts/1/links/2", params: {password: "foo"}]) do
29
+ link = Kloudless::Link.update(account_id: 1, link_id: 2, password: "foo")
30
+ assert_kind_of Kloudless::Link, link
31
+ end
32
+ end
33
+
34
+ def test_delete_link
35
+ Kloudless.http.expect(:delete, args: ["/accounts/1/links/2"]) do
36
+ link = Kloudless::Link.delete(account_id: 1, link_id: 2)
37
+ assert_kind_of Kloudless::Link, link
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,38 @@
1
+ require_relative "../test_helper"
2
+
3
+ class Kloudless::MultipartUploadTest < Minitest::Test
4
+ def test_init_multipart_upload
5
+ Kloudless.http.expect(:post, returns: {"id" => "foo"}, args:["/accounts/1/multipart", params: {overwrite: true}]) do
6
+ upload = Kloudless::MultipartUpload.init(account_id: 1, overwrite: true)
7
+ assert_kind_of Kloudless::MultipartUpload, upload
8
+ end
9
+ end
10
+
11
+ def test_retrieve_multipart_upload
12
+ Kloudless.http.expect(:get, returns: {"id" => "foo"}, args:["/accounts/1/multipart/2"]) do
13
+ upload = Kloudless::MultipartUpload.retrieve(account_id: 1, multipart_id: 2)
14
+ assert_kind_of Kloudless::MultipartUpload, upload
15
+ end
16
+ end
17
+
18
+ def test_upload_multipart_upload
19
+ Kloudless.http.expect(:put, args: ["/accounts/1/multipart/2", params: {part_number: 1}]) do
20
+ upload = Kloudless::MultipartUpload.upload(account_id: 1, multipart_id: 2, part_number: 1)
21
+ assert_kind_of Kloudless::MultipartUpload, upload
22
+ end
23
+ end
24
+
25
+ def test_finalize_multipart_upload
26
+ Kloudless.http.expect(:post, args: ["/accounts/1/multipart/2/complete"]) do
27
+ upload = Kloudless::MultipartUpload.finalize(account_id: 1, multipart_id: 2)
28
+ assert_kind_of Kloudless::MultipartUpload, upload
29
+ end
30
+ end
31
+
32
+ def test_abort_multipart_upload
33
+ Kloudless.http.expect(:delete, args: ["/accounts/1/multipart/2"]) do
34
+ upload = Kloudless::MultipartUpload.abort(account_id: 1, multipart_id: 2)
35
+ assert_kind_of Kloudless::MultipartUpload, upload
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,11 @@
1
+ require_relative "../test_helper"
2
+
3
+ class Kloudless::TeamTest < Minitest::Test
4
+ def test_list_teams
5
+ Kloudless.http.expect(:get, returns: {"objects" => [{}]}, args: ["/accounts/1/team", params: {}]) do
6
+ teams = Kloudless::Team.list(account_id: 1)
7
+ assert_kind_of Kloudless::Collection, teams
8
+ assert_kind_of Kloudless::Team, teams.first
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,25 @@
1
+ require_relative "test_helper"
2
+
3
+ class KloudlessTest < Minitest::Test
4
+ def test_unauthorized
5
+ assert_raises Kloudless::ForbiddenError do
6
+ Kloudless::Account.list
7
+ end
8
+ end
9
+
10
+ def test_authorize_api_key
11
+ Kloudless.authorize(api_key: "API_KEY")
12
+ Kloudless.http.mock_response(Struct.new(:body).new('{}')) do
13
+ Kloudless::Account.list
14
+ assert_equal "ApiKey API_KEY", Kloudless::HTTP.last_request["Authorization"]
15
+ end
16
+ end
17
+
18
+ def test_authorize_account_key
19
+ Kloudless.authorize(account_key: "ACCOUNT_KEY")
20
+ Kloudless.http.mock_response(Struct.new(:body).new('{}')) do
21
+ Kloudless::Account.list
22
+ assert_equal "AccountKey ACCOUNT_KEY", Kloudless::HTTP.last_request["Authorization"]
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,17 @@
1
+ require_relative "../lib/kloudless"
2
+ require "minitest/autorun"
3
+ require "minitest/mock"
4
+ require "byebug"
5
+
6
+ module Kloudless
7
+ class Test
8
+ def teardown
9
+ unmock
10
+ end
11
+
12
+ def unmock
13
+ Kloudless.http = Kloudless::HTTP
14
+ Kloudless.http.mock_response = nil
15
+ end
16
+ end
17
+ end
metadata ADDED
@@ -0,0 +1,150 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kloudless
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jerry Cheung
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: byebug
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ - jollyjerry@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - TODO.md
82
+ - kloudless.gemspec
83
+ - lib/kloudless.rb
84
+ - lib/kloudless/account.rb
85
+ - lib/kloudless/account_key.rb
86
+ - lib/kloudless/collection.rb
87
+ - lib/kloudless/error.rb
88
+ - lib/kloudless/event.rb
89
+ - lib/kloudless/file.rb
90
+ - lib/kloudless/folder.rb
91
+ - lib/kloudless/http.rb
92
+ - lib/kloudless/link.rb
93
+ - lib/kloudless/model.rb
94
+ - lib/kloudless/multipart_upload.rb
95
+ - lib/kloudless/team.rb
96
+ - lib/kloudless/version.rb
97
+ - script/changelog
98
+ - script/package
99
+ - script/release
100
+ - test/kloudless/account_key_test.rb
101
+ - test/kloudless/account_test.rb
102
+ - test/kloudless/collection_test.rb
103
+ - test/kloudless/error_test.rb
104
+ - test/kloudless/event_test.rb
105
+ - test/kloudless/file_test.rb
106
+ - test/kloudless/folder_test.rb
107
+ - test/kloudless/http_test.rb
108
+ - test/kloudless/link_test.rb
109
+ - test/kloudless/multipart_upload_test.rb
110
+ - test/kloudless/team_test.rb
111
+ - test/kloudless_test.rb
112
+ - test/test_helper.rb
113
+ homepage: https://github.com/jch/kloudless
114
+ licenses:
115
+ - MIT
116
+ metadata: {}
117
+ post_install_message:
118
+ rdoc_options: []
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '2.1'
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ requirements: []
132
+ rubyforge_project:
133
+ rubygems_version: 2.2.2
134
+ signing_key:
135
+ specification_version: 4
136
+ summary: Kloudless API client
137
+ test_files:
138
+ - test/kloudless/account_key_test.rb
139
+ - test/kloudless/account_test.rb
140
+ - test/kloudless/collection_test.rb
141
+ - test/kloudless/error_test.rb
142
+ - test/kloudless/event_test.rb
143
+ - test/kloudless/file_test.rb
144
+ - test/kloudless/folder_test.rb
145
+ - test/kloudless/http_test.rb
146
+ - test/kloudless/link_test.rb
147
+ - test/kloudless/multipart_upload_test.rb
148
+ - test/kloudless/team_test.rb
149
+ - test/kloudless_test.rb
150
+ - test/test_helper.rb