<commit_before># -*- coding: utf-8 -*-

"""
    tests.apiserver
    ~~~~~~~~~~~~

    Tests cobra.api

    :author:    40huo <<EMAIL>>
    :homepage:  https://github.com/wufeifei/cobra
    :license:   MIT, see LICENSE for more details.
    :copyright: Copyright (c) 2017 Feei. All rights reserved
"""

import requests
import json
import multiprocessing
import time
import os
import shutil
import socket
from cobra.config import project_directory
from cobra.api import start

p = multiprocessing.Process(target=start, args=('127.0.0.1', 5000, False))
p.start()
time.sleep(1)

config_path = os.path.join(project_directory, 'config')
template_path = os.path.join(project_directory, 'config.template')
shutil.copyfile(template_path, config_path)


def test_add_job():
    url = "http://127.0.0.1:5000/api/add"
    post_data = {
        "key": "your_secret_key",
        "target": ["https://github.com/shadowsocks/shadowsocks.git"],
    }
    headers = {
        "Content-Type": "application/json",
    }
    re = requests.post(url=url, data=json.dumps(post_data), headers=headers)
    assert "1001" in re.text
    assert "Add scan job successfully" in re.text
    assert "sid" in re.text


def test_job_status():
    url = "http://127.0.0.1:5000/api/status"
    post_data = {
        "key": "your_secret_key",
        "sid": 24,
    }
    headers = {
        "Content-Type": "application/json",
    }
    re = requests.post(url=url, data=json.dumps(post_data), headers=headers)
    assert "1004" in re.text
    assert "msg" in re.text
    assert "sid" in re.text
    assert "status" in re.text
    assert "report" in re.text


def test_result_data():
    url = 'http://127.0.0.1:5000/api/list'
    post_data = {
        'sid': 24,
    }
    headers = {
        "Content-Type": "application/json",
    }
    re = requests.post(url=url, data=json.dumps(post_data), headers=headers)
    assert '1002' in re.text
    assert 'No such target' in re.text


def test_result_detail():
    url = 'http://127.0.0.1:5000/api/detail'
    post_data = {
        'target': 'https://github.com/shadowsocks/shadowsocks.git',
        'file_path': 'setup.py',
    }
    headers = {
        "Content-Type": "application/json",
    }
    re = requests.post(url=url, data=json.dumps(post_data), headers=headers)
    filename = '/tmp/cobra/git/shadowsocks/shadowsocks/setup.py'
    if os.path.exists(filename):
        assert '1001' in re.text
        assert '<EMAIL>' in re.text
    else:
        assert '1002' in re.text
        assert 'No such file' in re.text


def test_index():
    url = 'http://127.0.0.1:5000/'
    re = requests.get(url=url)
    assert 'Github / Gitlab' in re.text

    url = 'http://127.0.0.1:5000/?sid=abcde'
    re = requests.get(url=url)
    assert 'scan id does not exist!' in re.text


def test_close_api():
    os.remove(config_path)
    p.terminate()
    p.join()
<commit_msg>fix test case for API<commit_after># -*- coding: utf-8 -*-

"""
    tests.apiserver
    ~~~~~~~~~~~~

    Tests cobra.api

    :author:    40huo <<EMAIL>huo.cn>
    :homepage:  https://github.com/wufeifei/cobra
    :license:   MIT, see LICENSE for more details.
    :copyright: Copyright (c) 2017 Feei. All rights reserved
"""

import requests
import json
import multiprocessing
import time
import os
import shutil
import socket
from cobra.config import project_directory
from cobra.api import start

p = multiprocessing.Process(target=start, args=('127.0.0.1', 5000, False))
p.start()
time.sleep(1)

config_path = os.path.join(project_directory, 'config')
template_path = os.path.join(project_directory, 'config.template')
shutil.copyfile(template_path, config_path)


def test_add_job():
    url = "http://127.0.0.1:5000/api/add"
    post_data = {
        "key": "your_secret_key",
        "target": ["https://github.com/shadowsocks/shadowsocks.git"],
    }
    headers = {
        "Content-Type": "application/json",
    }
    re = requests.post(url=url, data=json.dumps(post_data), headers=headers)
    assert "1001" in re.text
    assert "Add scan job successfully" in re.text
    assert "sid" in re.text


def test_job_status():
    url = "http://127.0.0.1:5000/api/status"
    post_data = {
        "key": "your_secret_key",
        "sid": 24,
    }
    headers = {
        "Content-Type": "application/json",
    }
    re = requests.post(url=url, data=json.dumps(post_data), headers=headers)
    assert "1004" in re.text
    assert "msg" in re.text
    assert "sid" in re.text
    assert "status" in re.text
    assert "report" in re.text


def test_result_data():
    url = 'http://127.0.0.1:5000/api/list'
    post_data = {
        'sid': 24,
    }
    headers = {
        "Content-Type": "application/json",
    }
    re = requests.post(url=url, data=json.dumps(post_data), headers=headers)
    assert '1002' in re.text
    assert 'No such target' in re.text


def test_result_detail():
    url = 'http://127.0.0.1:5000/api/detail'
    post_data = {
        'sid': 'abcdeft',
        'file_path': 'setup.py',
    }
    headers = {
        "Content-Type": "application/json",
    }
    re = requests.post(url=url, data=json.dumps(post_data), headers=headers)
    assert '1002' in re.text
    assert 'No such target' in re.text


def test_index():
    url = 'http://127.0.0.1:5000/'
    re = requests.get(url=url)
    assert 'Github / Gitlab' in re.text

    url = 'http://127.0.0.1:5000/?sid=abcde'
    re = requests.get(url=url)
    assert 'scan id does not exist!' in re.text


def test_close_api():
    os.remove(config_path)
    p.terminate()
    p.join()

{'fraction_non_alphanumeric': 0.1516843811925779, 'fraction_numerical': 0.03602954422626554, 'mean_word_length': 2.8475398475398475, 'pattern_counts': {'":': 16, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 5, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 14, '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': '5414042', 'n_tokens_mistral': 2086, 'n_tokens_neox': 1946, 'n_words': 433}