Skip to content

Commit b4191ed

Browse files
authored
Merge pull request #1 from GrantBirki/code-coverage
Code coverage
2 parents af3cb24 + f6526e8 commit b4191ed

18 files changed

+89
-20
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@ lib/**/spec/
2121
# secrets
2222
creds.env
2323
*.pem
24+
25+
coverage/

CONTRIBUTING.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ This project uses a highly opinionated dependency vendoring strategy. This strat
99
3. The `script/bootstrap` command installs vendored dependencies with `SHARDS_CACHE_PATH="vendor/.cache/shards" shards install ...` to ensure that each project has its own cache and does not interfere with other crystal projects
1010
4. The `script/update` command will re-vendor all dependencies and update the vendored dependencies in the repository. This will always result is changes to all dependencies, even if the version has not changed. This is to ensure that the vendored dependencies are always up to date and can be used to build the project.
1111

12+
## Testing 🧪
13+
14+
- All code must have unit tests
15+
- 100% code coverage is enforced
16+
- Acceptance tests are used to test the application as a whole
17+
1218
## Development Commands 💻
1319

1420
### Setup

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# kemal-hmac
22

3-
[![test](https://github.com/GrantBirki/kemal-hmac/actions/workflows/test.yml/badge.svg)](https://github.com/GrantBirki/kemal-hmac/actions/workflows/test.yml) [![build](https://github.com/GrantBirki/kemal-hmac/actions/workflows/build.yml/badge.svg)](https://github.com/GrantBirki/kemal-hmac/actions/workflows/build.yml) [![lint](https://github.com/GrantBirki/kemal-hmac/actions/workflows/lint.yml/badge.svg)](https://github.com/GrantBirki/kemal-hmac/actions/workflows/lint.yml) [![acceptance](https://github.com/GrantBirki/kemal-hmac/actions/workflows/acceptance.yml/badge.svg)](https://github.com/GrantBirki/kemal-hmac/actions/workflows/acceptance.yml)
3+
[![test](https://github.com/GrantBirki/kemal-hmac/actions/workflows/test.yml/badge.svg)](https://github.com/GrantBirki/kemal-hmac/actions/workflows/test.yml) [![build](https://github.com/GrantBirki/kemal-hmac/actions/workflows/build.yml/badge.svg)](https://github.com/GrantBirki/kemal-hmac/actions/workflows/build.yml) [![lint](https://github.com/GrantBirki/kemal-hmac/actions/workflows/lint.yml/badge.svg)](https://github.com/GrantBirki/kemal-hmac/actions/workflows/lint.yml) [![acceptance](https://github.com/GrantBirki/kemal-hmac/actions/workflows/acceptance.yml/badge.svg)](https://github.com/GrantBirki/kemal-hmac/actions/workflows/acceptance.yml) [![coverage](./docs/assets/coverage.svg)](./docs/assets/coverage.svg)
44

55
HMAC middleware for Crystal's [kemal](https://github.com/kemalcr/kemal) framework

docs/assets/coverage.svg

Lines changed: 25 additions & 0 deletions
Loading

script/require_spec.cr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require "../spec/**"

script/test

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ source script/setup-env $@
66

77
echo -e "🧪 ${BLUE}running unit tests...${OFF}"
88

9-
crystal spec
9+
# if -s is passed anywhere in the arguments, run without code coverage checks
10+
if [[ $@ == *"-s"* || "$CI" == "true" && "$OSTYPE" == "darwin"* ]]; then
11+
echo "skipping code coverage checks"
12+
crystal spec --exclude-warnings $SHARDS_INSTALL_PATH
13+
else
14+
script/test_with_coverage $@
15+
echo -e "\n"
16+
fi
1017

1118
echo -e "${GREEN}tests complete!${OFF}"

script/test_with_coverage

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
source script/setup-env $@
6+
7+
KCOV_BIN="kcov"
8+
9+
if [[ -f "$DIR/bin/kcov" && "$CI" == "true" ]]; then
10+
KCOV_BIN="$DIR/bin/kcov"
11+
fi
12+
13+
# check to ensure both crystal and shards are installed
14+
if ! [ -x "$(command -v $KCOV_BIN)" ]; then
15+
echo -e "${RED}Error${OFF}: kcov is not installed"
16+
echo "Please install kcov to use code coverage reports:"
17+
echo " - macOS: https://formulae.brew.sh/formula/kcov"
18+
echo " - other: https://github.com/SimonKagstrom/kcov/blob/c71d42f1cadddbd2da38c1101d71ef2b84065c7b/INSTALL.md"
19+
exit 1
20+
fi
21+
22+
# only build the require_spec binary if it's not already built
23+
if [ ! -f "$DIR/bin/require_spec" ]; then
24+
echo "building require_spec binary..."
25+
crystal build "$DIR/script/require_spec.cr" -D skip-integration -o "$DIR/bin/require_spec" --exclude-warnings $SHARDS_INSTALL_PATH
26+
fi
27+
28+
$KCOV_BIN --clean --include-path=$DIR/src "$DIR/coverage" "$DIR/bin/require_spec"
29+
30+
CRYSTAL_OPTS="" crystal tool unreachable "$DIR/script/require_spec.cr" --check

shard.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ version: 2.0
22
shards:
33
ameba:
44
git: https://github.com/crystal-ameba/ameba.git
5-
version: 1.6.2
5+
version: 1.6.3
66

77
backtracer:
88
git: https://github.com/sija/backtracer.cr.git

spec/kemal-hmac/kemal-hmac_spec.cr

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
require "../spec_helper"
22

33
describe "Kemal::Hmac" do
4-
# it "goes to next handler with correct credentials" do
5-
# hmac_handler = Kemal::Hmac::Handler.new()
6-
# request = HTTP::Request.new(
7-
# "GET",
8-
# "/",
9-
# headers: HTTP::Headers{"foo" => "bar"},
10-
# )
4+
it "uses a custom handler with path matching and sends a request to an endpoint that does not require hmac auth" do
5+
hmac_handler = SpecAuthHandler.new
6+
request = HTTP::Request.new(
7+
"GET",
8+
"/health"
9+
)
10+
11+
io, context = create_request_and_return_io_and_context(hmac_handler, request)
12+
response = HTTP::Client::Response.from_io(io, decompress: false)
13+
response.status_code.should eq 404
14+
context.kemal_authorized_client?.should be nil
15+
end
1116

12-
# io, context = create_request_and_return_io_and_context(hmac_handler, request)
13-
# response = HTTP::Client::Response.from_io(io, decompress: false)
14-
# response.status_code.should eq 404
15-
# context.kemal_authorized_client?.should eq("serdar")
16-
# end
17+
it "calls the hmac_auth helper method without errors" do
18+
hmac_auth[0].to_s.should contain "Kemal::Hmac::Handler"
19+
end
1720

1821
it "returns 401 when a header is provided but it is not for hmac auth" do
1922
hmac_handler = Kemal::Hmac::Handler.new

src/kemal-hmac/handler.cr

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,5 @@ module Kemal::Hmac
9494
def missing_hmac_headers(headers : Hash(String, String?)) : Array(String)
9595
headers.select { |_, v| v.nil? }.keys
9696
end
97-
98-
def authorize?(value) : String?
99-
username, password = Base64.decode_string(value[BASIC.size + 1..-1]).split(":")
100-
@credentials.authorize?(username, password)
101-
end
10297
end
10398
end

0 commit comments

Comments
 (0)