Skip to content

Commit fa7cafb

Browse files
authored
Integration test (#6)
* Test oauth guard * sleep a bit so guard can start * test curl response and token * get id token * restart test * remove auth header * fix invalid token test * Add test for wrong subject * use local variable name in if statement
1 parent 46ad356 commit fa7cafb

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

.github/workflows/build.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ on:
77
- main
88
workflow_dispatch:
99

10+
1011
env:
1112
REGISTRY: ghcr.io
1213
IMAGE_NAME: ${{ github.repository }}

.github/workflows/pr.yml

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,75 @@ jobs:
4545

4646
- name: Helm Lint
4747
run: helm lint charts/radix-oauth-guard
48+
49+
integration-test:
50+
name: Integration test
51+
runs-on: ubuntu-latest
52+
permissions:
53+
id-token: write
54+
contents: read
55+
steps:
56+
- uses: actions/checkout@v4
57+
- uses: actions/setup-go@v4
58+
with:
59+
go-version-file: 'go.mod'
60+
- name: Install dependencies
61+
run: go mod download
62+
- name: Install oauth guard
63+
run: go install .
64+
65+
- uses: actions/github-script@v7
66+
id: get-id-token
67+
with:
68+
script: return await core.getIDToken()
69+
result-encoding: string
70+
- uses: actions/github-script@v7
71+
id: get-invalid-aud-id-token
72+
with:
73+
script: return await core.getIDToken("invalid-audience")
74+
result-encoding: string
75+
- name: Test Auth
76+
env:
77+
LOG_PRETTY: True
78+
LOG_LEVEL: Trace
79+
ISSUER: "https://token.actions.githubusercontent.com"
80+
AUDIENCE: "https://github.com/equinor"
81+
SUBJECTS: repo:equinor/radix-oauth-guard:pull_request,testmultiplesubjects
82+
GH_TOKEN: ${{ steps.get-id-token.outputs.result }}
83+
INVALID_GH_TOKEN: ${{ steps.get-invalid-aud-id-token.outputs.result }}
84+
run: |
85+
function assert() {
86+
local token="${1}"
87+
local expected="${2}"
88+
local msg="${3}"
89+
90+
CURL_RESPONSE=$(curl --write-out '%{http_code}' --output /dev/null --silent --header "Authorization: Bearer ${token}" http://localhost:8000/auth)
91+
printf "Test: %15s: Result %s == %s: " "${msg}" "${expected}" "${CURL_RESPONSE}"
92+
93+
if [ "${expected}" != "${CURL_RESPONSE}" ]; then
94+
printf "Failed\n\n"
95+
exit 255
96+
fi
97+
98+
printf "OK\n\n"
99+
}
100+
101+
radix-oauth-guard &
102+
GO_PID=$!
103+
sleep 2s
104+
105+
assert "${GH_TOKEN}" "200" "Valid token is OK"
106+
assert "" "401" "No token is unauthorized"
107+
assert "ABCD${GH_TOKEN}" "401" "Invalid token is unauthorized"
108+
assert "${INVALID_GH_TOKEN}" "401" "Wrong Audience is unauthorized"
109+
110+
kill -9 $GO_PID
111+
112+
# Test different subject
113+
SUBJECTS=WRONG_SUBJECT radix-oauth-guard &
114+
GO_PID=$!
115+
sleep 2s
116+
117+
assert "${GH_TOKEN}" "403" "Wrong Subject is Forbidden"
118+
kill -9 $GO_PID
119+
:

0 commit comments

Comments
 (0)