<commit_before>package authenticator

import (
	"net/http"
	"net/url"
	"testing"

	"github.com/abbot/go-http-auth"
	"github.com/fxnn/gone/authenticator/bruteblocker"
)

func TestMiddlewareHandler_noAuthentication(t *testing.T) {
	var requestAuth = newMockAuthenticator()
	var sessionAuth = newMockAuthenticator()
	var sut = sutWithRequestAuthAndSessionAuth(requestAuth, sessionAuth)
	var delegate = http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {})
	var req = blankRequest()
	var rsp = newMockResponseWriter()

	sut.MiddlewareHandler(delegate).ServeHTTP(rsp, req)

	if requestAuth.IsAuthenticated(req) || sessionAuth.IsAuthenticated(req) {
		t.Fatalf("No authentication expected")
	}
}

func TestMiddlewareHandler_copiesUserId(t *testing.T) {
	var requestAuth = newMockAuthenticator()
	var sessionAuth = newMockAuthenticator()
	var sut = sutWithRequestAuthAndSessionAuth(requestAuth, sessionAuth)
	var delegate = http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {})
	var req = blankRequest()
	var rsp = newMockResponseWriter()

	sessionAuth.SetUserID(rsp, req, "Aladdin")
	sut.MiddlewareHandler(delegate).ServeHTTP(rsp, req)

	if userID := requestAuth.UserID(req); userID != "Aladdin" {
		t.Fatalf("Expected Aladdin to be authenticated, but was '%v'", userID)
	}
}

func TestLoginHandler_rightCredentials(t *testing.T) {
	var requestAuth = newMockAuthenticator()
	var sessionAuth = newMockAuthenticator()
	var sut = sutWithBruteblockerRequestAuthSessionAuthAndBasicAuth(requestAuth, sessionAuth, aladdinsSecret)<commit_msg>fix #21: tests for #4 require ssl connection<commit_after>package authenticator

import (
	"net/http"
	"net/url"
	"testing"

	"github.com/abbot/go-http-auth"
	"github.com/fxnn/gone/authenticator/bruteblocker"
)

func TestMiddlewareHandler_noAuthentication(t *testing.T) {
	var requestAuth = newMockAuthenticator()
	var sessionAuth = newMockAuthenticator()
	var sut = sutWithRequestAuthAndSessionAuth(requestAuth, sessionAuth)
	var delegate = http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {})
	var req = blankRequest()
	var rsp = newMockResponseWriter()

	sut.MiddlewareHandler(delegate).ServeHTTP(rsp, req)

	if requestAuth.IsAuthenticated(req) || sessionAuth.IsAuthenticated(req) {
		t.Fatalf("No authentication expected")
	}
}

func TestMiddlewareHandler_copiesUserId(t *testing.T) {
	var requestAuth = newMockAuthenticator()
	var sessionAuth = newMockAuthenticator()
	var sut = sutWithRequestAuthAndSessionAuth(requestAuth, sessionAuth)
	var delegate = http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {})
	var req = blankRequest()
	var rsp = newMockResponseWriter()

	sessionAuth.SetUserID(rsp, req, "Aladdin")
	sut.MiddlewareHandler(delegate).ServeHTTP(rsp, req)

	if userID := requestAuth.UserID(req); userID != "Aladdin" {
		t.Fatalf("Expected Aladdin to be authenticated, but was '%v'", userID)
	}
}

func TestLoginHandler_existingHeader(t *testing.T) {
	var requestAuth = newMockAuthenticator()
	var sessionAuth = newMockAuthenticator()
	var sut = sutWithBruteblockerRequestAuthSessionAuthAndBasicAuth(requestAuth, sessionAuth, aladdinsSecret)
	var req = requestWithAladdinUser()
	var rsp = newMockResponseWriter()

	sut.loginRequiresHeader = "Test-Header"
	req.Header.Add("Test-Header", "existing")
	sut.LoginHandler().ServeHTTP(rsp, req)

	if userID := requestAuth.UserID(req); userID != "Aladdin" {
		t.Fatalf("Expected request user to be Aladdin, but was '%v'", userID)
	}
	if userID := sessionAuth.UserID(req); userID != "Aladdin" {
		t.Fatalf("Expected session user to be Aladdin, but was '%v'", userID)
	}
	if rsp.status != http.StatusFound {
		t.Fatalf("Expected status authorized, was '%v'", rsp.status)
	}
}

func TestLoginHandler_missingHeader(t *testing.T) {
	var requestAuth = newMockAuthenticator()
	var sessionAuth = newMockAuthenticator()
	var sut = sutWithBruteblockerRequestAuthSessionAuthAndBasicAuth(requestAuth, sessionAuth, aladdinsSecret)
	var req = requestWithAladdinUser()
	var rsp = newMockResponseWriter()

	sut.loginRequiresHeader = "test"
	sut.LoginHandler().ServeHTTP(rsp, req)

	if userID := requestAuth.UserID(req); userID != "" {
		t.Fatalf("Expected request user to be empty, but was '%v'", userID)
	}
	if userID := sessionAuth.UserID(req); userID != "" {
		t.Fatalf("Expected session user to be empty, but was '%v'", userID)
	}
	if rsp.status != http.StatusBadRequest {
		t.Fatalf("Expected status bad request, was '%v'", rsp.status)
	}
}

func TestLoginHandler_rightCredentials(t *testing.T) {
	var requestAuth = newMockAuthenticator()
	var sessionAuth = newMockAuthenticator()
	var sut = sutWithBruteblockerRequestAuthSessionAuthAndBasicAuth(requestAuth, sessionAuth, aladdinsSecret)
{'fraction_non_alphanumeric': 0.11400925536390408, 'fraction_numerical': 0.0006310475389145982, 'mean_word_length': 6.337962962962963, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '6169817', 'n_tokens_mistral': 1582, 'n_tokens_neox': 1569, 'n_words': 331}