Skip to content

test: add direct unit tests for grafana config normalization and auth…#1186

Merged
muddlebee merged 2 commits intoTracer-Cloud:mainfrom
PrakharJain345:test/grafana-config
May 2, 2026
Merged

test: add direct unit tests for grafana config normalization and auth…#1186
muddlebee merged 2 commits intoTracer-Cloud:mainfrom
PrakharJain345:test/grafana-config

Conversation

@PrakharJain345
Copy link
Copy Markdown
Contributor

Fixes #879

Description

Added comprehensive unit tests for app/services/grafana/config.py.

Changes Made

  • Added test coverage for Grafana config service
  • Ensured robustness of configuration validation logic

🧪 Test Coverage Includes

  • URL Normalization
    Verifies trailing slashes and whitespace are stripped from instance_url

  • Local Anonymous Auth
    Tests security logic allowing localhost / 127.0.0.1 without tokens while enforcing tokens for hosted instances

  • Config Validation (is_configured)
    Ensures config works for both token-based and local setups


Demo / Verification

Verified locally using pytest:

python -m pytest tests/services/test_grafana_config.py -v
# Output: 12 passed in 2.11s

Code Understanding and AI Usage

Did you use AI assistance?

  • No, I wrote all the code myself
  • Yes, I used AI assistance

If yes:

  • I have reviewed every line of the generated code
  • I can explain the logic of my implementation
  • I have tested edge cases
  • I have modified the code to follow project standards

Implementation Approach

Implemented a test suite in tests/services/test_grafana_config.py using pytest.mark.parametrize to efficiently cover multiple edge cases related to authentication and URL formatting. This ensures strict enforcement of local anonymous auth rules and predictable behavior of the configuration model.


Checklist

  • PR title is clear and linked to the issue
  • Self-review completed
  • Code is understandable and well-structured
  • Changes are tested thoroughly
  • Edge cases considered
  • Tests added for core functionality
  • Code follows project conventions

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented May 1, 2026

Greptile Summary

This PR adds 12 unit tests for GrafanaAccountConfig in tests/services/test_grafana_config.py, covering URL normalisation, uses_local_anonymous_auth, and is_configured. All test assertions are logically correct against the implementation; the parametrized cases accurately reflect the branching conditions in config.py. Remaining findings are P2 style suggestions only.

Confidence Score: 5/5

Safe to merge — all test assertions are correct and no production code is changed.

Only P2 findings remain: a missing module docstring and a suggestion to split the single URL normalisation test case into parametrized variants. Neither affects test correctness or production behaviour.

No files require special attention.

Important Files Changed

Filename Overview
tests/services/test_grafana_config.py New unit test file covering GrafanaAccountConfig URL normalization, uses_local_anonymous_auth, and is_configured; all assertions are logically correct against the implementation; minor style improvements possible.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[GrafanaAccountConfig input]
    A --> B[Normalize URL: strip whitespace and trailing slashes]
    B --> C{URL empty?}
    C -- Yes --> D[is_configured equals False]
    C -- No --> E{Credential present?}
    E -- Yes --> F[is_configured equals True]
    E -- No --> G{Host is loopback?}
    G -- Yes --> H[local_anon equals True, is_configured equals True]
    G -- No --> I[local_anon equals False, is_configured equals False]
Loading

Reviews (1): Last reviewed commit: "test: add direct unit tests for grafana ..." | Re-trigger Greptile

Comment on lines +1 to +5
from __future__ import annotations

import pytest

from app.services.grafana.config import GrafanaAccountConfig
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Missing module-level docstring

Every other Grafana test file in tests/services/ opens with a module docstring (e.g. test_grafana_loki.py, test_grafana_mimir.py). Adding one here would be consistent with the project convention and make the file's intent clear.

Context Used: Testing conventions and patterns (source)

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Comment on lines +8 to +15
def test_grafana_config_normalizes_instance_url() -> None:
"""Test that trailing slashes and whitespace are stripped from the instance URL."""
config = GrafanaAccountConfig(
account_id="test-acc",
instance_url=" https://grafana.example.com/ ",
read_token="secret",
)
assert config.instance_url == "https://grafana.example.com"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Single combined case for URL normalisation

The test bundles leading whitespace and a trailing slash into one input, so a regression that only strips slashes (but not whitespace) would still pass. Consider adding parametrized cases that isolate each normalisation step — trailing slash only, whitespace only, multiple consecutive slashes, and a URL that is already clean — consistent with how the other two tests in this file exercise individual conditions.

@muddlebee muddlebee merged commit 5325315 into Tracer-Cloud:main May 2, 2026
10 checks passed
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 2, 2026

🛸 Aliens watching our repo just upgraded @PrakharJain345's threat level to: do not engage — too competent. 👽


👋 Join us on Discord - OpenSRE : hang out, contribute, or hunt for features and issues. Everyone's welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add direct unit tests for app/services/grafana/config.py

2 participants