Skip to content

Commit

Permalink
make sure an object is read only after doggy push
Browse files Browse the repository at this point in the history
  • Loading branch information
ElvinEfendi committed Nov 24, 2016
1 parent a83283c commit c2b6ad6
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 5 deletions.
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
source 'https://rubygems.org'

gemspec

group :test do
gem 'mocha'
gem 'webmock'
end
18 changes: 17 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,42 @@ PATH
GEM
remote: https://rubygems.org/
specs:
addressable (2.5.0)
public_suffix (~> 2.0, >= 2.0.2)
axiom-types (0.1.1)
descendants_tracker (~> 0.0.4)
ice_nine (~> 0.11.0)
thread_safe (~> 0.3, >= 0.3.1)
coercible (1.0.0)
descendants_tracker (~> 0.0.1)
crack (0.4.3)
safe_yaml (~> 1.0.0)
descendants_tracker (0.0.4)
thread_safe (~> 0.3, >= 0.3.1)
equalizer (0.0.11)
hashdiff (0.2.3)
ice_nine (0.11.2)
json (1.8.3)
metaclass (0.0.4)
minitest (5.9.0)
mocha (1.1.0)
metaclass (~> 0.0.1)
parallel (1.6.2)
public_suffix (2.0.4)
rake (10.5.0)
rugged (0.23.3)
safe_yaml (1.0.4)
thor (0.19.1)
thread_safe (0.3.5)
virtus (1.0.5)
axiom-types (~> 0.1)
coercible (~> 1.0)
descendants_tracker (~> 0.0, >= 0.0.3)
equalizer (~> 0.0, >= 0.0.9)
webmock (1.22.6)
addressable (>= 2.3.6)
crack (>= 0.3.2)
hashdiff

PLATFORMS
ruby
Expand All @@ -41,7 +55,9 @@ DEPENDENCIES
bundler (~> 1.10)
doggy!
minitest
mocha
rake (~> 10.0)
webmock

BUNDLED WITH
1.12.5
1.13.6
7 changes: 7 additions & 0 deletions lib/doggy/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ module Doggy
class Model
include Virtus.model

attribute :read_only, Boolean

# This stores the path on disk. We don't define it as a model attribute so
# it doesn't get serialized.
attr_accessor :path
Expand Down Expand Up @@ -193,8 +195,13 @@ def validate
# NotImplemented
end

def ensure_read_only!
self.read_only = true
end

def save
ensure_managed_emoji!
ensure_read_only!
validate

body = JSON.dump(to_h)
Expand Down
18 changes: 18 additions & 0 deletions test/doggy/model_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,24 @@ class DummyModelWithRoot < DummyModel
self.root = :dash
end

def test_create
model = Doggy::Models::Dashboard.new({'dash' => {'title' => 'Pipeline'}})
stub_request(:post, 'https://app.datadoghq.com/api/v1/dash?api_key=api_key_123&application_key=app_key_345').
with(:body => "{\"description\":null,\"graphs\":[],\"id\":null,\"read_only\":true,\"template_variables\":[],\"title\":\"Pipeline 🐶\"}").
to_return(:status => 200, :body => "{\"id\":1}")
File.expects(:open).with(Doggy.object_root.join('dash-1.json'), 'w')
model.save
end

def test_update
model = Doggy::Models::Monitor.new(id: 1, title: 'Some test', name: 'Monitor name')
stub_request(:put, "https://app.datadoghq.com/api/v1/monitor/1?api_key=api_key_123&application_key=app_key_345").
with(:body => "{\"id\":1,\"message\":null,\"multi\":null,\"name\":\"Monitor name 🐶\",\"options\":{},"\
"\"org_id\":null,\"query\":null,\"read_only\":true,\"tags\":[],\"type\":null}").
to_return(:status => 200)
model.save
end

def test_sort_by_key
h = { b: [ {d: 1, a: 2}, {x: 1, p: 3, y: 5} ], a: 3 }
expected = { a: 3, b: [ {a: 2, d: 1}, {p: 3, x: 1, y: 5} ] }
Expand Down
18 changes: 14 additions & 4 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,20 @@

require 'minitest/pride'
require 'minitest/autorun'
require 'minitest/unit'
require 'mocha/mini_test'
require 'webmock/minitest'

def load_fixture(fixture_path)
path = File.expand_path("fixtures/#{ fixture_path }", "#{ __FILE__ }/../")
raw = File.read(path)
class MiniTest::Test
def before_setup
Doggy.stubs(:secrets).returns({'datadog_api_key' => 'api_key_123', 'datadog_app_key' => 'app_key_345'})
super
end

JSON.parse(raw)
def load_fixture(fixture_path)
path = File.expand_path("fixtures/#{ fixture_path }", "#{ __FILE__ }/../")
raw = File.read(path)

JSON.parse(raw)
end
end

0 comments on commit c2b6ad6

Please sign in to comment.