<commit_before>"""Tests for the dashboard API"""
import os
import requests

""" Assume username and password for testing """
TEST_USER = os.environ.get("USER")
TEST_PASSWD = <PASSWORD>.get("USER")

API_URL = "http://localhost:8000/api"


def test_api_root():
    """Access to the api root"""
    r = requests.get(API_URL)
    assert r.status_code == requests.codes.ok


def test_auth():
    """Attempt to access a resource without authentication"""

    r = requests.get(API_URL)
    api = r.json()

    for endpoint in api:
        r = requests.get(api[endpoint])
        assert r.status_code == 401


def test_post_metric():

    metric = {
                "metric": "test1",
                "description": "Test metric insertion",
                "units": "test",
                "minimum": "8",
                "design": "5",
                "stretch": "3",
                "user": "5"
             }

    r = requests.get(API_URL)
    api = r.json()

    r = requests.post(api['metric'], json=metric,
                      auth=(TEST_USER, TEST_PASSWD))
    assert r.status_code == 201

    r.close()


def test_post_job():

    r = requests.get(API_URL)
    api = r.json()

    job = {
            "name": "ci_cfht",
            "build": "1",
            "url": "https://ci.lsst.codes/job/ci_cfht/1/",
            "measurements": [{"metric": "test1", "value": 3.0}],
            "status": 0
          }

    r = requests.post(api['job'], json=job,
                      auth=(TEST_USER, TEST_PASSWD))

    assert r.status_code == 201<commit_msg>Update API with new endpoints/packages model<commit_after>"""Tests for the dashboard API"""
import os
import requests

""" Assume username and password for testing """
TEST_USER = os.environ.get("USER")
TEST_PASSWD = os.environ.get("USER")

API_URL = "http://localhost:8000/api"


def test_api_root():
    """Access to the api root"""
    r = requests.get(API_URL)
    assert r.status_code == requests.codes.ok


def test_auth():
    """Attempt to access a resource without authentication"""

    r = requests.get(API_URL)
    api = r.json()

    for endpoint in api:
        r = requests.get(api[endpoint])
        assert r.status_code == 401


def test_post_metric():

    metric = {
                "metric": "test1",
                "description": "Test metric insertion",
                "units": "test",
                "minimum": "8",
                "design": "5",
                "stretch": "3",
                "user": "5"
             }

    r = requests.get(API_URL)
    api = r.json()

    r = requests.post(api['metrics'], json=metric,
                      auth=(TEST_USER, TEST_PASSWD))
    assert r.status_code == 201

    r.close()


def test_post_job():

    r = requests.get(API_URL)
    api = r.json()

    packages = [
        {'name': 'afw',
         'git_url': 'https://github.com/lsst/afw.git',
         'git_commit': 'fc355a99abe3425003b0e5fbe1e13a39644b1e95',
         'git_branch': 'master',
         'build_version': 'b2000'}]
    measurements = [{"metric": "test1", "value": 3.0}]
    job = {
        "name": "ci_cfht",
        "build": "1",
        "ci_url": "https://ci.lsst.codes/job/ci_cfht/1/",
        "measurements": measurements,
        "packages": packages,
        "status": 0
    }

    r = requests.post(api['jobs'], json=job,
                      auth=(TEST_USER, TEST_PASSWD))

    assert r.status_code == 201
{'fraction_non_alphanumeric': 0.15384615384615385, 'fraction_numerical': 0.022607163828537873, 'mean_word_length': 2.091651542649728, 'pattern_counts': {'":': 29, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3186368', 'n_tokens_mistral': 1160, 'n_tokens_neox': 1064, 'n_words': 249}