codedematrix/datacollection
0
1#!/bin/bash2# =============================================================================3# NCA Data Collection System — Automated API Test Suite4# Run: bash testing/api_test.sh [BASE_URL]5# Default BASE_URL: http://localhost:80006# For live deployment: bash testing/api_test.sh https://codedematrix-datacollection.hf.space7# =============================================================================8 9BASE_URL="${1:-http://localhost:8000}"10API="$BASE_URL/api/v1"11PASS=012FAIL=013SKIP=014 15# Colours16GREEN='\033[0;32m'17RED='\033[0;31m'18YELLOW='\033[1;33m'19CYAN='\033[0;36m'20NC='\033[0m'21 22pass() { echo -e " ${GREEN}✓ PASS${NC} $1"; ((PASS++)); }23fail() { echo -e " ${RED}✗ FAIL${NC} $1"; echo -e " ${RED}$2${NC}"; ((FAIL++)); }24skip() { echo -e " ${YELLOW}– SKIP${NC} $1 — $2"; ((SKIP++)); }25section() { echo -e "\n${CYAN}▶ $1${NC}"; }26 27# Helper: check HTTP status28check_status() {29 local label="$1" expected="$2" actual="$3" body="$4"30 if [ "$actual" == "$expected" ]; then31 pass "$label (HTTP $actual)"32 else33 fail "$label" "Expected HTTP $expected, got HTTP $actual. Body: $body"34 fi35}36 37echo ""38echo "=================================================="39echo " NCA Data Collection — API Test Suite"40echo " Target: $BASE_URL"41echo " $(date)"42echo "=================================================="43 44# =============================================================================45section "1. Health Check"46# =============================================================================47 48RESP=$(curl -s -o /dev/null -w "%{http_code}" "$BASE_URL/")49if [ "$RESP" == "200" ] || [ "$RESP" == "302" ] || [ "$RESP" == "404" ]; then50 pass "Server reachable (HTTP $RESP)"51else52 fail "Server unreachable" "HTTP $RESP — is the server running at $BASE_URL?"53fi54 55# =============================================================================56section "2. Authentication"57# =============================================================================58 59# Login as NCA Admin60LOGIN_RESP=$(curl -s -X POST "$API/auth/login/" \61 -H "Content-Type: application/json" \62 -d '{"email":"admin@nca.org.gh","password":"testpass123"}')63 64NCA_TOKEN=$(echo "$LOGIN_RESP" | grep -o '"access":"[^"]*"' | cut -d'"' -f4)65 66if [ -n "$NCA_TOKEN" ]; then67 pass "NCA Admin login returns access token"68else69 fail "NCA Admin login" "No token returned. Response: $LOGIN_RESP"70fi71 72# Login as Provider73PROV_RESP=$(curl -s -X POST "$API/auth/login/" \74 -H "Content-Type: application/json" \75 -d '{"email":"dataentry@vodafone.com.gh","password":"testpass123"}')76 77PROV_TOKEN=$(echo "$PROV_RESP" | grep -o '"access":"[^"]*"' | cut -d'"' -f4)78 79if [ -n "$PROV_TOKEN" ]; then80 pass "Provider login returns access token"81else82 fail "Provider login" "No token returned. Response: $PROV_RESP"83fi84 85# Bad credentials should return 40186BAD_STATUS=$(curl -s -o /dev/null -w "%{http_code}" -X POST "$API/auth/login/" \87 -H "Content-Type: application/json" \88 -d '{"email":"wrong@email.com","password":"wrongpass"}')89check_status "Bad credentials rejected" "401" "$BAD_STATUS" ""90 91# Get current user profile92if [ -n "$NCA_TOKEN" ]; then93 ME_STATUS=$(curl -s -o /dev/null -w "%{http_code}" "$API/auth/me/" \94 -H "Authorization: Bearer $NCA_TOKEN")95 check_status "GET /auth/me/ returns profile" "200" "$ME_STATUS" ""96fi97 98# Unauthenticated request should be rejected99UNAUTH=$(curl -s -o /dev/null -w "%{http_code}" "$API/providers/")100check_status "Unauthenticated request rejected" "401" "$UNAUTH" ""101 102# =============================================================================103section "3. Providers"104# =============================================================================105 106if [ -n "$NCA_TOKEN" ]; then107 # List providers108 PROV_LIST=$(curl -s "$API/providers/" -H "Authorization: Bearer $NCA_TOKEN")109 PROV_STATUS=$(curl -s -o /dev/null -w "%{http_code}" "$API/providers/" \110 -H "Authorization: Bearer $NCA_TOKEN")111 check_status "GET /providers/ list" "200" "$PROV_STATUS" ""112 113 PROV_COUNT=$(echo "$PROV_LIST" | grep -o '"count":[0-9]*' | cut -d: -f2)114 if [ -n "$PROV_COUNT" ] && [ "$PROV_COUNT" -gt 0 ]; then115 pass "Providers list has $PROV_COUNT results"116 else117 fail "Providers list empty" "Expected seeded providers. Response: $PROV_LIST"118 fi119 120 # Get first provider ID121 PROV_ID=$(echo "$PROV_LIST" | grep -o '"id":[0-9]*' | head -1 | cut -d: -f2)122 123 # Search providers124 SEARCH_STATUS=$(curl -s -o /dev/null -w "%{http_code}" "$API/providers/?search=Vodafone" \125 -H "Authorization: Bearer $NCA_TOKEN")126 check_status "GET /providers/?search=Vodafone" "200" "$SEARCH_STATUS" ""127 128 # Filter by category129 CAT_STATUS=$(curl -s -o /dev/null -w "%{http_code}" "$API/providers/?category=MNO" \130 -H "Authorization: Bearer $NCA_TOKEN")131 check_status "GET /providers/?category=MNO" "200" "$CAT_STATUS" ""132 133 # Get single provider134 if [ -n "$PROV_ID" ]; then135 SINGLE_STATUS=$(curl -s -o /dev/null -w "%{http_code}" "$API/providers/$PROV_ID/" \136 -H "Authorization: Bearer $NCA_TOKEN")137 check_status "GET /providers/$PROV_ID/" "200" "$SINGLE_STATUS" ""138 fi139 140 # Provider cannot see other providers141 if [ -n "$PROV_TOKEN" ]; then142 PROV_LIST_AS_PROV=$(curl -s "$API/providers/" -H "Authorization: Bearer $PROV_TOKEN")143 PROV_LIST_COUNT=$(echo "$PROV_LIST_AS_PROV" | grep -o '"count":[0-9]*' | cut -d: -f2)144 if [ -n "$PROV_LIST_COUNT" ]; then145 pass "Provider user can only see own provider data (count: $PROV_LIST_COUNT)"146 fi147 fi148else149 skip "Providers tests" "No NCA token available"150fi151 152# =============================================================================153section "4. Submissions (Expected)"154# =============================================================================155 156if [ -n "$NCA_TOKEN" ]; then157 SUB_LIST=$(curl -s "$API/expected-submissions/" -H "Authorization: Bearer $NCA_TOKEN")158 SUB_STATUS=$(curl -s -o /dev/null -w "%{http_code}" "$API/expected-submissions/" \159 -H "Authorization: Bearer $NCA_TOKEN")160 check_status "GET /expected-submissions/ list" "200" "$SUB_STATUS" ""161 162 SUB_COUNT=$(echo "$SUB_LIST" | grep -o '"count":[0-9]*' | cut -d: -f2)163 if [ -n "$SUB_COUNT" ] && [ "$SUB_COUNT" -gt 0 ]; then164 pass "Submissions list has $SUB_COUNT results"165 else166 fail "Submissions list empty" "Expected seeded submissions. Run seed_data first."167 fi168 169 # Get first submission ID170 SUB_ID=$(echo "$SUB_LIST" | grep -o '"id":[0-9]*' | head -1 | cut -d: -f2)171 172 # Search submissions173 SEARCH_STATUS=$(curl -s -o /dev/null -w "%{http_code}" "$API/expected-submissions/?search=Vodafone" \174 -H "Authorization: Bearer $NCA_TOKEN")175 check_status "Search submissions by provider name" "200" "$SEARCH_STATUS" ""176 177 # Filter by status178 FILT_STATUS=$(curl -s -o /dev/null -w "%{http_code}" "$API/expected-submissions/?workflow_status=DRAFT" \179 -H "Authorization: Bearer $NCA_TOKEN")180 check_status "Filter submissions by workflow_status=DRAFT" "200" "$FILT_STATUS" ""181 182 # Get single submission183 if [ -n "$SUB_ID" ]; then184 SINGLE_STATUS=$(curl -s -o /dev/null -w "%{http_code}" "$API/expected-submissions/$SUB_ID/" \185 -H "Authorization: Bearer $NCA_TOKEN")186 check_status "GET /expected-submissions/$SUB_ID/" "200" "$SINGLE_STATUS" ""187 fi188 189 # Provider sees only own submissions190 if [ -n "$PROV_TOKEN" ]; then191 PROV_SUBS=$(curl -s "$API/expected-submissions/" -H "Authorization: Bearer $PROV_TOKEN")192 PROV_SUB_COUNT=$(echo "$PROV_SUBS" | grep -o '"count":[0-9]*' | cut -d: -f2)193 NCA_SUB_COUNT=$(echo "$SUB_LIST" | grep -o '"count":[0-9]*' | cut -d: -f2)194 if [ -n "$PROV_SUB_COUNT" ] && [ "$PROV_SUB_COUNT" -lt "$NCA_SUB_COUNT" ]; then195 pass "Provider sees fewer submissions than NCA ($PROV_SUB_COUNT vs $NCA_SUB_COUNT — RBAC working)"196 else197 fail "Provider RBAC on submissions" "Provider count ($PROV_SUB_COUNT) should be less than NCA count ($NCA_SUB_COUNT)"198 fi199 fi200else201 skip "Submission tests" "No NCA token available"202fi203 204# =============================================================================205section "5. Compliance Flags"206# =============================================================================207 208if [ -n "$NCA_TOKEN" ]; then209 COMP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" "$API/compliance/flags/" \210 -H "Authorization: Bearer $NCA_TOKEN")211 check_status "GET /compliance/flags/" "200" "$COMP_STATUS" ""212 213 # Filter by status214 OPEN_STATUS=$(curl -s -o /dev/null -w "%{http_code}" "$API/compliance/flags/?status=OPEN" \215 -H "Authorization: Bearer $NCA_TOKEN")216 check_status "GET /compliance/flags/?status=OPEN" "200" "$OPEN_STATUS" ""217 218 COMP_BODY=$(curl -s "$API/compliance/flags/" -H "Authorization: Bearer $NCA_TOKEN")219 FLAG_ID=$(echo "$COMP_BODY" | grep -o '"id":[0-9]*' | head -1 | cut -d: -f2)220 221 if [ -n "$FLAG_ID" ]; then222 # Acknowledge a flag223 ACK_STATUS=$(curl -s -o /dev/null -w "%{http_code}" -X PATCH \224 "$API/compliance/flags/$FLAG_ID/acknowledge/" \225 -H "Authorization: Bearer $NCA_TOKEN")226 check_status "PATCH /compliance/flags/$FLAG_ID/acknowledge/" "200" "$ACK_STATUS" ""227 else228 skip "Flag acknowledge test" "No flags found — run flag_missing_data command first"229 fi230else231 skip "Compliance tests" "No NCA token available"232fi233 234# =============================================================================235section "6. Users"236# =============================================================================237 238if [ -n "$NCA_TOKEN" ]; then239 USERS_STATUS=$(curl -s -o /dev/null -w "%{http_code}" "$API/auth/users/" \240 -H "Authorization: Bearer $NCA_TOKEN")241 check_status "GET /auth/users/ list" "200" "$USERS_STATUS" ""242 243 # Provider cannot list all users244 if [ -n "$PROV_TOKEN" ]; then245 PROV_USERS=$(curl -s -o /dev/null -w "%{http_code}" "$API/auth/users/" \246 -H "Authorization: Bearer $PROV_TOKEN")247 if [ "$PROV_USERS" == "403" ] || [ "$PROV_USERS" == "401" ]; then248 pass "Provider blocked from listing users (HTTP $PROV_USERS)"249 else250 fail "Provider RBAC on users" "Expected 403, got $PROV_USERS"251 fi252 fi253else254 skip "Users tests" "No NCA token available"255fi256 257# =============================================================================258section "7. Exports"259# =============================================================================260 261if [ -n "$NCA_TOKEN" ] && [ -n "$PROV_ID" ] && [ -n "$PERIOD_ID" ]; then262 EXPORT_STATUS=$(curl -s -o /dev/null -w "%{http_code}" -X POST "$API/exports/csv/" \263 -H "Authorization: Bearer $NCA_TOKEN" \264 -H "Content-Type: application/json" \265 -d "{\"provider_id\":$PROV_ID}")266 check_status "POST /exports/csv/" "200" "$EXPORT_STATUS" ""267else268 skip "Export test" "Need provider ID and period ID from earlier tests"269fi270 271# =============================================================================272section "8. Audit Log"273# =============================================================================274 275if [ -n "$NCA_TOKEN" ]; then276 AUDIT_STATUS=$(curl -s -o /dev/null -w "%{http_code}" "$API/audit/" \277 -H "Authorization: Bearer $NCA_TOKEN")278 check_status "GET /audit/ log accessible to NCA" "200" "$AUDIT_STATUS" ""279fi280 281# =============================================================================282echo ""283echo "=================================================="284echo " RESULTS"285echo "=================================================="286echo -e " ${GREEN}Passed:${NC} $PASS"287echo -e " ${RED}Failed:${NC} $FAIL"288echo -e " ${YELLOW}Skipped:${NC} $SKIP"289TOTAL=$((PASS + FAIL + SKIP))290echo " Total: $TOTAL"291echo ""292if [ "$FAIL" -eq 0 ]; then293 echo -e " ${GREEN}All tests passed! ✓${NC}"294else295 echo -e " ${RED}$FAIL test(s) failed. Review output above.${NC}"296fi297echo "=================================================="298echo ""299 