Skip to content

Commit

Permalink
Spec all the CLI methods
Browse files Browse the repository at this point in the history
Change Access serialization from numeric to string
  • Loading branch information
dscottboggs committed May 21, 2019
1 parent e23e797 commit 9a78d32
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 35 deletions.
22 changes: 17 additions & 5 deletions run-tests.sh
@@ -1,10 +1,22 @@
#!/bin/sh

set -ue
set -e

build=no
if [ "$1" = 'and' ] && [ "$2" = 'build' ]; then
shift; shift
build=yes
fi

set -e

do_build() {
printf "building...\r"
shards build > /dev/null
printf "building...done\nAll OK\n"
}

crystal tool format
ameba
KEMAL_ENV=test crystal spec
printf "building...\r"
shards build > /dev/null
printf "building...done\nAll OK\n"
KEMAL_ENV=test crystal spec $@
[ $build = yes ] && do_build
73 changes: 71 additions & 2 deletions spec/cli_spec.cr
Expand Up @@ -261,9 +261,9 @@ module DppmRestApi::CLI
access: "Read",
data_dir: __DIR__
new_state = File.open permissions_file! do |file|
Config.from_json file
Config.from_json file.rewind
end
pp! permissions_file!
pp! new_state.groups.select { |grp| grp.id == 0 }
if su = new_state.groups.find { |group| group.id == 0 }
su.can_access?(
"/literally/anything",
Expand All @@ -287,5 +287,74 @@ module DppmRestApi::CLI
end
end
end
describe "#edit_group_query" do
it "throws an error when the group id is not a numeral" do
with_state_restore do
expect_raises InvalidGroupID do
edit_group_query group_id: "five",
path: "/doesnt/matter",
access: "Read",
data_dir: __DIR__,
key: "something",
add_glob: nil,
remove_glob: nil
end
end
end
it "throws an error if the group doesn't exist" do
with_state_restore do
expect_raises NoSuchGroup do
edit_group_query group_id: "5",
path: "/doesnt/matter",
access: "Read",
data_dir: __DIR__,
key: "something",
add_glob: nil,
remove_glob: nil
end
end
end
it "can add a glob to a query" do
with_state_restore do
edit_group_query group_id: "1000",
path: "/**",
access: "Create | Read | Delete",
data_dir: __DIR__,
key: "test-key",
add_glob: "test-glob",
remove_glob: nil
new_state = File.open permissions_file! do |file|
Config.from_json file
end
test_group = new_state.groups.find { |grp| grp.id == 1000 }.not_nil!
test_group.permissions["/**"]
.query_parameters["test-key"]?
.should eq ["test-glob"]
end
end
end
describe "#add_route" do
it "can add a path to a group's #permissions values" do
with_state_restore do
add_route group_id: "1000", access: "Read", path: "/some/path", data_dir: __DIR__
new_state = File.open permissions_file! do |file|
Config.from_json file
end
test_group = new_state.groups.find { |grp| grp.id == 1000 }.not_nil!
test_group.permissions["/some/path"]?.should_not be_nil
end
end
end
describe "#delete_group" do
it "can remove a group" do
with_state_restore do
delete_group group_id: "1000", data_dir: __DIR__
new_state = File.open permissions_file! do |file|
Config.from_json file
end
new_state.groups.find { |grp| grp.id == 1000 }.should be_nil
end
end
end
end
end
12 changes: 6 additions & 6 deletions spec/permissions.json
Expand Up @@ -5,7 +5,7 @@
"id": 0,
"permissions": {
"/**": {
"permissions": 15
"permissions": "Create | Read | Update | Delete"
}
}
},
Expand All @@ -14,7 +14,7 @@
"id": 500,
"permissions": {
"/**": {
"permissions": 15,
"permissions": "Create | Read | Update | Delete",
"query_parameters": {
"namespace": [
"default-namespace"
Expand All @@ -28,7 +28,7 @@
"id": 501,
"permissions": {
"/**": {
"permissions": 15,
"permissions": "Create | Read | Update | Delete",
"query_parameters": {
"namespace": [
"default-namespace"
Expand All @@ -42,7 +42,7 @@
"id": 502,
"permissions": {
"/**": {
"permissions": 15,
"permissions": "Create | Read | Update | Delete",
"query_parameters": {
"namespace": [
"default-namespace"
Expand All @@ -56,7 +56,7 @@
"id": 499,
"permissions": {
"/**": {
"permissions": 15,
"permissions": "Create | Read | Update | Delete",
"query_parameters": {
"namespace": [
"default-namespace"
Expand All @@ -70,7 +70,7 @@
"id": 1000,
"permissions": {
"/**": {
"permissions": 15,
"permissions": "Create | Read | Update | Delete",
"query_parameters": {
"namespace": [
"jim-oliver"
Expand Down
23 changes: 18 additions & 5 deletions src/access.cr
Expand Up @@ -12,19 +12,32 @@ enum DppmRestApi::Access : UInt8

def self.new(pull parser : JSON::PullParser)
case parser.kind
when :int then from_value parser.read_int
when :int
number = parser.read_int
puts "got numeric access to parse " + number.to_s
from_value number
when :string
read = parser.read_string
puts "got string access to parse " + read
if read.includes? '|'
value = Access::None
read.split('|').each { |substr| value |= parse substr }
read.split('|').each do |substr|
parsed = parse substr.strip
value |= parsed
puts "got substring '#{substr}' which was parsed to '#{parsed}'. Value is now '#{value}'"
end
value
else
parse? read
end
when :begin_array
value = Access::None
parser.read_array { value |= parse parser.read_string }
parser.read_array do
str = parser.read_string
parsed = parse str
value |= parsed
puts "got array element '#{str}' which was parsed to '#{parsed}'. Value is now '#{value}'"
end
value
else
raise JSON::ParseException.new <<-HERE, parser.line_number, parser.column_number
Expand All @@ -35,11 +48,11 @@ enum DppmRestApi::Access : UInt8
end

def to_json
value
'"' + to_s + '"'
end

def to_json(builder : JSON::Builder)
builder.number value
builder.string to_s
end

def self.from_json(value : Number)
Expand Down
56 changes: 39 additions & 17 deletions src/cli.cr
Expand Up @@ -424,34 +424,54 @@ module DppmRestApi::CLI
parsed_access = Access.parse?(access) ||
Access.from_value access.to_i? ||
raise InvalidAccessParam.new access
original_cfg = group.permissions[path]? || Config::Route.new parsed_access, {} of String => Array(String)
original_cfg = group.permissions[path]? || Config::Route.new parsed_access
original_cfg.permissions = parsed_access
group.permissions[path] = original_cfg
current_config.groups << group
current_config.write_to permissions_file
pp! permissions_file
pp! File.read permissions_file
end

def add_route(group_id, access, path, data_dir, **args)
required group_id, path, access, data_dir
group_id_number = group_id.to_i? || raise InvalidGroupID.new group_id
permissions_file = Path[data_dir, "permissions.json"]
current_config = File.open permissions_file do |file|
DppmRestApi::Config.from_json file
end
relevant_group = current_config.groups
.find { |grp| grp.id == group_id_number } || raise NoSuchGroup.new group_id_number
parsed_access = DppmRestApi::Access.parse access
route = DppmRestApi::Config::Route.new parsed_access
relevant_group.permissions[path] = route
current_config.write_to permissions_file
end

def edit_group_query(group_id, key, add_glob, remove_glob, path, data_dir, **args)
required group_id, key, path
required group_id, key, path, data_dir
group_id_number = group_id.to_i? || raise InvalidGroupID.new group_id
permissions_file = Path[required(:data_dir), "permissions.json"]
permissions_file = Path[data_dir, "permissions.json"]
current_config = File.open permissions_file do |file|
DppmRestApi::Config.from_json file
end
relevant_group = current_config.groups.find { |grp| grp.id == group_id_number }
relevant_group || raise NoSuchGroup.new group_id_number
if glob_to_add = add_glob
current_config.groups
.find { |grp| grp.id == group_id_number }
.try do |mn_grp|
mn_grp.permissions[path].query_parameters[key] << glob_to_add
if route = relevant_group.permissions[path]?
if route.query_parameters[key]?
relevant_group.permissions[path].query_parameters[key] << glob_to_add
else
relevant_group.permissions[path].query_parameters[key] = [glob_to_add]
end
else
raise NoRouteMatchForThisGroup.new path, group_id_number
end
end
if glob_to_rm = remove_glob
current_config.groups
.find { |grp| grp.id == group_id_number }
.try do |mn_grp|
mn_grp.permissions[path].query_parameters[key].delete glob_to_rm
end
relevant_group.permissions[path].query_parameters[key].delete glob_to_rm
if relevant_group.permissions[path].query_parameters[key].empty?
relevant_group.permissions[path].query_parameters.delete key
end
end
current_config.write_to permissions_file
end
Expand All @@ -469,10 +489,7 @@ module DppmRestApi::CLI

macro define_error(kind, message, *args, &block)
class {{kind.id}} < ::Exception
def initialize(
{% unless args.empty? %}
{{args.splat}}
{% end %})
def initialize({% unless args.empty? %} {{args.splat}} {% end %})
super {{message.id}}
end
{% if block %}
Expand All @@ -494,4 +511,9 @@ module DppmRestApi::CLI
define_error RequiredArgument, "the argument '#{@arg.gsub '_', '-'}' is required!", @arg : String do
property arg
end
define_error NoRouteMatchForThisGroup, <<-HERE, path, group_id
please use add-route to add a route before editing the
query parameters of that group. No existing permissions
data was found for the glob #{path} for the group #{group_id}
HERE
end

0 comments on commit 9a78d32

Please sign in to comment.