<commit_before>package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"log"
	"mime/multipart"
	"net/http"
	"net/http/httptest"
	"net/url"
	"reflect"
	"strings"
	"testing"
	"time"
)

func createDB(test string) {
	var err error
	err = openDB(fmt.Sprintf("file:%s?mode=memory&cache=shared", test))
	if err != nil {
		log.Fatal(err)
	}
}

func destroyDB() {
	db.Close()
}

func TestLoadDataWithNoResults(t *testing.T) {
	createDB("TestLoadDataWithNoResults")
	defer destroyDB()
	data, err := loadData(sqlFilter{})
	if err != nil {
		t.Fatalf("error from loadData: %v", err)
	}
	if len(data) != 0 {
		t.Errorf("expected len 0, got %v", len(data))
	}
}

func TestLoadTraceroutesWithNoResults(t *testing.T) {
	createDB("TestLoadTraceroutesWithNoResults")
	defer destroyDB()
	tr, err := loadTraceroutes()
	if err != nil {
		t.Fatal(err)
	}
	if len(tr) != 0 {
		t.Errorf("expected 0 results, got %v", len(tr))
	}
}

func TestLoadJobsWithNoResults(t *testing.T) {
	createDB("TestLoadJobsWithNoResults")
	defer destroyDB()
	data, err := loadJobs(sqlFilter{})
	if err != nil {
		t.Fatalf("error from loadJobs: %v", err)
	}
	if len(data) != 0 {
		t.Errorf("expected len 0, got %v", len(data))
	}
}

func TestSaveData(t *testing.T) {
	createDB("TestSaveData")
	defer destroyDB()
	results := []result{
		{IP: "192.0.2.1", Ports: []port{{Port: 80, Proto: "tcp", Status: "open"}}},
		{IP: "192.0.2.2", Ports: []port{{Port: 80, Proto: "tcp", Status: "open"}}},
		{IP: "192.0.2.3", Ports: []port{{Port: 80, Proto: "tcp", Status: "open"}}},
	}
	count, err := saveData(results, time.Now().UTC())
	if err != nil {
		t.Fatal(err)
	}
	if count != int64(len(results)) {
		t.Errorf("expected count %d, got %d", len(results), count)
	}

}

func TestSaveJob(t *testing.T) {
	createDB("TestSaveJob")
	defer destroyDB()
	id, err := saveJob("192.0.2.0/24", "80,443", "tcp", "<EMAIL>")
	if err != nil {
		t.Fatal(err)
	}
	if id != 1 {
		t.Errorf("expected ID 1, got %d", id)
	}
}

func TestResultData(t *testing.T) {
	createDB("TestResultData")
	defer destroyDB()
	want := scanData{Total: 0, Latest: 0, New: 0, LastSeen: time.Unix(0, 0).Unix(), Results: nil}
	data, err := resultData("", "", "")
	if err != nil {
		t.Fatal(err)
	}
	if !reflect.DeepEqual(data, want) {
		t.Errorf("want %+v, got %+v", want, data)
	}
}

func TestUpdateJob(t *testing.T) {
	createDB("TestUpdateJob")
	defer destroyDB()
	id, err := saveJob("192.0.2.0/24", "80,443", "tcp", "<EMAIL>")
	if err != nil {
		t.Fatal(err)
	}
	err = updateJob(string(id), 999)
	if err != nil {
		t.Errorf("error updating job: %v", err)
	}
}

// TestIndexHandlerWithoutAuth tests fetching the index page with
// authentication disabled
func TestIndexHandlerWithoutAuth(t *testing.T) {
	createDB("TestIndexHandlerWithoutAuth")
	defer destroyDB()

	// We can't go through the OAuth2 login flow in tests
	authDisabled = true

	setupTemplates()

	r := httptest.NewRequest("GET", "/", nil)
	w := httptest.NewRecorder()
	index(w, r)

	resp := w.Result()
	body, _ := ioutil.ReadAll(resp.Body)
	if resp.StatusCode != http.StatusOK {
		t.Errorf("expected status 200, got %v: %s", resp.StatusCode, body)
	}
}

// TestIPsHandler tests that we get expected JSON data
func TestIPsHandler(t *testing.T) {
	createDB("TestIPsHandler")
	defer destroyDB()

	r := httptest.NewRequest("GET", "/ips.json", nil)
	w := httptest.NewRecorder()
	ips(w, r)

	resp := w.Result()
	body, _ := ioutil.ReadAll(resp.Body)
	if resp.StatusCode != http.StatusOK {
		t.Errorf("expected status 200, got %v: %s", resp.StatusCode, body)<commit_msg>Tests: Move auth and template setup to init()<commit_after>package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"log"
	"mime/multipart"
	"net/http"
	"net/http/httptest"
	"net/url"
	"reflect"
	"strings"
	"testing"
	"time"
)

func init() {
	// We can't go through the OAuth2 login flow in tests
	authDisabled = true

	setupTemplates()
}

func createDB(test string) {
	var err error
	err = openDB(fmt.Sprintf("file:%s?mode=memory&cache=shared", test))
	if err != nil {
		log.Fatal(err)
	}
}

func destroyDB() {
	db.Close()
}

func TestLoadDataWithNoResults(t *testing.T) {
	createDB("TestLoadDataWithNoResults")
	defer destroyDB()
	data, err := loadData(sqlFilter{})
	if err != nil {
		t.Fatalf("error from loadData: %v", err)
	}
	if len(data) != 0 {
		t.Errorf("expected len 0, got %v", len(data))
	}
}

func TestLoadTraceroutesWithNoResults(t *testing.T) {
	createDB("TestLoadTraceroutesWithNoResults")
	defer destroyDB()
	tr, err := loadTraceroutes()
	if err != nil {
		t.Fatal(err)
	}
	if len(tr) != 0 {
		t.Errorf("expected 0 results, got %v", len(tr))
	}
}

func TestLoadJobsWithNoResults(t *testing.T) {
	createDB("TestLoadJobsWithNoResults")
	defer destroyDB()
	data, err := loadJobs(sqlFilter{})
	if err != nil {
		t.Fatalf("error from loadJobs: %v", err)
	}
	if len(data) != 0 {
		t.Errorf("expected len 0, got %v", len(data))
	}
}

func TestSaveData(t *testing.T) {
	createDB("TestSaveData")
	defer destroyDB()
	results := []result{
		{IP: "192.0.2.1", Ports: []port{{Port: 80, Proto: "tcp", Status: "open"}}},
		{IP: "192.0.2.2", Ports: []port{{Port: 80, Proto: "tcp", Status: "open"}}},
		{IP: "192.0.2.3", Ports: []port{{Port: 80, Proto: "tcp", Status: "open"}}},
	}
	count, err := saveData(results, time.Now().UTC())
	if err != nil {
		t.Fatal(err)
	}
	if count != int64(len(results)) {
		t.Errorf("expected count %d, got %d", len(results), count)
	}

}

func TestSaveJob(t *testing.T) {
	createDB("TestSaveJob")
	defer destroyDB()
	id, err := saveJob("192.0.2.0/24", "80,443", "tcp", "<EMAIL>")
	if err != nil {
		t.Fatal(err)
	}
	if id != 1 {
		t.Errorf("expected ID 1, got %d", id)
	}
}

func TestResultData(t *testing.T) {
	createDB("TestResultData")
	defer destroyDB()
	want := scanData{Total: 0, Latest: 0, New: 0, LastSeen: time.Unix(0, 0).Unix(), Results: nil}
	data, err := resultData("", "", "")
	if err != nil {
		t.Fatal(err)
	}
	if !reflect.DeepEqual(data, want) {
		t.Errorf("want %+v, got %+v", want, data)
	}
}

func TestUpdateJob(t *testing.T) {
	createDB("TestUpdateJob")
	defer destroyDB()
	id, err := saveJob("192.0.2.0/24", "80,443", "tcp", "<EMAIL>")
	if err != nil {
		t.Fatal(err)
	}
	err = updateJob(string(id), 999)
	if err != nil {
		t.Errorf("error updating job: %v", err)
	}
}

// TestIndexHandlerWithoutAuth tests fetching the index page with
// authentication disabled
func TestIndexHandlerWithoutAuth(t *testing.T) {
	createDB("TestIndexHandlerWithoutAuth")
	defer destroyDB()

	r := httptest.NewRequest("GET", "/", nil)
	w := httptest.NewRecorder()
	index(w, r)

	resp := w.Result()
	body, _ := ioutil.ReadAll(resp.Body)
	if resp.StatusCode != http.StatusOK {
		t.Errorf("expected status 200, got %v: %s", resp.StatusCode, body)
	}
}

// TestIPsHandler tests that we get expected JSON data
func TestIPsHandler(t *testing.T) {
	createDB("TestIPsHandler")
	defer destroyDB()

	r := httptest.NewRequest("GET", "/ips.json", nil)
	w := httptest.NewRecorder()
	ips(w, r)

	resp := w.Result()
	body, _ := ioutil.ReadAll(resp.Body)
	if resp.StatusCode != http.StatusOK {
		t.Errorf("expected status 200, got %v: %s", resp.StatusCode, body)
{'fraction_non_alphanumeric': 0.18595157657657657, 'fraction_numerical': 0.021114864864864864, 'mean_word_length': 4.302238805970149, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 10, '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': '6457774', 'n_tokens_mistral': 2971, 'n_tokens_neox': 2826, 'n_words': 670}