Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Team action improvements. #94

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 26 additions & 11 deletions spec/cb/completion_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ private class CompletionTestClient < CB::Client
end

def get_teams
[Team.new("def", "my team", false, "manager")]
[Factory.team(**{"id": "def", "name": "my team", "is_personal": false, "role": "manager"})]
end

def get_firewall_rules(id)
Expand Down Expand Up @@ -658,34 +658,49 @@ Spectator.describe CB::Completion do

# cb team info
result = parse("cb team info ")
expect(result).to eq ["def\tmy team"]
expect(result).to have_option "--format"
expect(result).to have_option "--team"
expect(result).to have_option "--no-header"

result = parse("cb team info def ")
expect(result).to eq [] of String
result = parse("cb team info --team def ")
expect(result).to_not have_option "--team"
expect(result).to have_option "--format"
expect(result).to have_option "--no-header"

result = parse("cb team info --team def --format ")
expect(result).to have_option "list"
expect(result).to have_option "json"
expect(result).to have_option "table"

# cb team update
result = parse("cb team update ")
expect(result).to eq ["def\tmy team"]
expect(result).to have_option "--billing-email"
expect(result).to have_option "--confirm"
expect(result).to have_option "--enforce-sso"
expect(result).to have_option "--format"
expect(result).to have_option "--name"
expect(result).to have_option "--team"

result = parse("cb team update def ")
result = parse("cb team update --team def ")
expect(result).to have_option "--billing-email"
expect(result).to have_option "--enforce-sso"
expect(result).to have_option "--name"

result = parse("cb team update def --enforce-sso ")
result = parse("cb team update --team def --enforce-sso ")
expect(result).to eq ["false", "true"]

result = parse("cb team update def --enforce-sso true ")
result = parse("cb team update --team def --enforce-sso true ")
expect(result).to_not have_option "--enforce-sso"
expect(result).to have_option "--billing-email"
expect(result).to have_option "--name"

# cb team destroy
result = parse("cb team destroy ")
expect(result).to eq ["def\tmy team"]
expect(result).to have_option "--confirm"
expect(result).to have_option "--team"

result = parse("cb team destroy def ")
expect(result).to eq [] of String
result = parse("cb team destroy --team def ")
expect(result).to have_option "--confirm"
end

it "completes team-member" do
Expand Down
213 changes: 213 additions & 0 deletions spec/cb/team_spec.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,74 @@
require "../spec_helper"

Spectator.describe TeamCreate do
subject(action) { described_class.new client: client, output: IO::Memory.new }

mock_client

let(team) { Factory.team }

describe "#validate" do
it "ensures required arguments are presents" do
expect(&.validate).to raise_error Program::Error, /Missing required argument/

action.name = team.name
expect(&.validate).to be_true
end
end

describe "#call" do
before_each {
action.name = team.name
expect(client).to receive(:create_team).and_return team
}

it "confirms team created (default output)" do
action.call
expect(&.output.to_s).to eq "Created team #{team.id} (#{team.name})\n"
end

it "outputs json" do
action.format = Format::JSON
action.call

expected = <<-EXPECTED
{
"id": "l2gnkxjv3beifk6abkraerv7de",
"name": "Test Team",
"is_personal": false,
"role": "admin",
"enforce_sso": false,
"billing_email": "[email protected]"
}\n
EXPECTED

expect(&.output.to_s).to eq expected
end

it "raises invalid output format error" do
action.format = Format::Table
expect(&.call).to raise_error Program::Error, /Invalid format: table/
end
end
end

Spectator.describe TeamDestroy do
subject(action) { described_class.new client: client, output: IO::Memory.new }

mock_client

let(team) { Factory.team }

describe "#validate" do
it "ensures required arguments are presents" do
expect(&.validate).to raise_error Program::Error, /Missing required argument/

action.team_id = team.id
expect(&.validate).to be_true
end
end
end

Spectator.describe TeamInfo do
subject(action) { described_class.new client: client, output: IO::Memory.new }

Expand All @@ -25,5 +94,149 @@ Spectator.describe TeamInfo do

expect(&.output.to_s).to eq expected
end

it "outputs as json" do
end
end
end

Spectator.describe TeamList do
subject(action) { described_class.new client: client, output: IO::Memory.new }

mock_client

let(team) { Factory.team }

describe "#call" do
before_each {
expect(client).to receive(:get_teams).and_return [team]
}

it "outputs table" do
action.call

expected = <<-EXPECTED
ID Name Role Billing Email Enforce SSO
l2gnkxjv3beifk6abkraerv7de Test Team Admin [email protected] disabled \n
EXPECTED

expect(&.output.to_s).to eq expected
end

it "outputs table without header" do
action.show_header = false
action.call

expected = <<-EXPECTED
l2gnkxjv3beifk6abkraerv7de Test Team Admin [email protected] disabled \n
EXPECTED

expect(&.output.to_s).to eq expected
end

it "outputs json" do
action.format = Format::JSON
action.call

expected = <<-EXPECTED
{
"teams": [
{
"id": "l2gnkxjv3beifk6abkraerv7de",
"name": "Test Team",
"is_personal": false,
"role": "admin",
"enforce_sso": false,
"billing_email": "[email protected]"
}
]
}\n
EXPECTED

expect(&.output.to_s).to eq expected
end
end
end

Spectator.describe TeamUpdate do
subject(action) { described_class.new client: client, output: IO::Memory.new }

mock_client

let(team) { Factory.team }

describe "#validate" do
it "ensures required arguments are present" do
expect(&.validate).to raise_error Program::Error, /Missing required argument/

action.team_id = team.id
expect(&.validate).to be_true
end
end

describe "#call" do
before_each {
action.team_id = team.id
action.confirmed = true
action.enforce_sso = true

expect(client).to receive(:update_team).and_return Factory.team(**{"enforce_sso": true})
}

it "outputs list" do
action.call

expected = <<-EXPECTED
ID: l2gnkxjv3beifk6abkraerv7de
Name: Test Team
Role: Admin
Billing Email: [email protected]
Enforce SSO: enabled \n
EXPECTED

expect(&.output.to_s).to eq expected
end

it "outputs table" do
action.format = Format::Table
action.call

expected = <<-EXPECTED
ID Name Role Billing Email Enforce SSO
l2gnkxjv3beifk6abkraerv7de Test Team Admin [email protected] enabled \n
EXPECTED

expect(&.output.to_s).to eq expected
end

it "outputs table without header" do
action.format = Format::Table
action.show_header = false
action.call

expected = <<-EXPECTED
l2gnkxjv3beifk6abkraerv7de Test Team Admin [email protected] enabled \n
EXPECTED

expect(&.output.to_s).to eq expected
end

it "outputs json" do
action.format = Format::JSON
action.call

expected = <<-EXPECTED
{
"id": "l2gnkxjv3beifk6abkraerv7de",
"name": "Test Team",
"is_personal": false,
"role": "admin",
"enforce_sso": true,
"billing_email": "[email protected]"
}\n
EXPECTED

expect(&.output.to_s).to eq expected
end
end
end
2 changes: 1 addition & 1 deletion spec/factory.cr
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ module Factory
is_personal: false,
role: "admin",
billing_email: "[email protected]",
enforce_sso: nil,
enforce_sso: false,
}.merge(params)

CB::Client::Team.new **params
Expand Down
8 changes: 6 additions & 2 deletions src/cb/action.cr
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ module CB

# Note: unlike the other macros, this one does not create a nilable boolean,
# and instead creates one that defaults to false
macro bool_setter(property)
property {{property}} : Bool = false
macro bool_setter(property, default = false)
property {{property}} : Bool = {{default}}

def {{property}}=(str : String)
case str.downcase
Expand Down Expand Up @@ -150,6 +150,10 @@ module CB
raise Error.new "Invalid #{field.colorize.bold}: '#{value.to_s.colorize.red}'"
end

private def raise_invalid_format(format)
raise Error.new "Invalid format: #{format.to_s.downcase}"
end

private def check_required_args
missing = [] of String
yield missing
Expand Down
Loading