<commit_before>package api_v1

import (
	"testing"

	"scal/settings"
	. "scal/user_lib"

	"github.com/golang/mock/gomock"
	"github.com/ilius/is"
	. "github.com/ilius/ripo"
)

var origAdminEmails = settings.ADMIN_EMAILS

func resetAdminEmails() {
	settings.ADMIN_EMAILS = origAdminEmails
}

func TestAdminGetStats_Unauthenticated(t *testing.T) {
	is := is.New(t)

	ctrl := gomock.NewController(t)
	defer ctrl.Finish()
	mockReq := NewMockExtendedRequest(ctrl)
	var req ExtendedRequest = mockReq

	mockReq.EXPECT().Header(gomock.Eq("Authorization"))

	res, err := AdminGetStats(req)
	rpcErr := err.(RPCError)
	is.Err(rpcErr)
	is.Equal(rpcErr.Code(), Unauthenticated)
	is.Nil(res)
}

func TestAdminGetStats_NotAdmin(t *testing.T) {
	defer testUserMapClear()

	authorization := "abcd"
	email := "<EMAIL>"
	testUserMap = map[string]*UserModel{
		authorization: &UserModel{
			Email: email,
		},
	}

	ctrl := gomock.NewController(t)
	defer ctrl.Finish()
	mockReq := NewMockExtendedRequest(ctrl)
	var req ExtendedRequest = mockReq
	mockReq.EXPECT().Header(gomock.Eq("Authorization")).Return(authorization)

	is := is.New(t)
	res, err := AdminGetStats(req)
	rpcErr := err.(RPCError)
	is.Err(rpcErr)
	is.Equal(rpcErr.Code(), PermissionDenied)
	is.Equal(rpcErr.Message(), "you are not admin")
	is.Nil(res)
}

func TestAdminGetStats_EmailNotConfirmed(t *testing.T) {
	defer testUserMapClear()
	defer resetAdminEmails()

	authorization := "abcd"
	email := "<EMAIL>"
	testUserMap = map[string]*UserModel{
		authorization: &UserModel{
			Email: email,
		},
	}
	settings.ADMIN_EMAILS = []string{email}

	ctrl := gomock.NewController(t)
	defer ctrl.Finish()
	mockReq := NewMockExtendedRequest(ctrl)
	var req ExtendedRequest = mockReq
	mockReq.EXPECT().Header(gomock.Eq("Authorization")).Return(authorization)

	is := is.New(t)
	res, err := AdminGetStats(req)
	rpcErr := err.(RPCError)
	is.Equal(rpcErr.Code(), PermissionDenied)
	is.Equal(rpcErr.Message(), "email is not confirmed")
	is.Err(rpcErr)
	is.Nil(res)
}

func TestAdminGetStats_OK(t *testing.T) {
	defer testUserMapClear()
	defer resetAdminEmails()

	authorization := "abcd"
	email := "<EMAIL>"
	testUserMap = map[string]*UserModel{
		authorization: &UserModel{
			Email:          email,
			EmailConfirmed: true,
		},
	}
	settings.ADMIN_EMAILS = []string{email}

	ctrl := gomock.NewController(t)
	defer ctrl.Finish()
	mockReq := NewMockExtendedRequest(ctrl)
	var req ExtendedRequest = mockReq
	mockReq.EXPECT().Header(gomock.Eq("Authorization")).Return(authorization)

	is := is.New(t)
	res, err := AdminGetStats(req)
	is.NotErr(err)
	is.NotNil(res)
	is.NotNil(res.Data)
	dataMap := res.Data.(map[string]interface{})
	locked_resource_count := dataMap["locked_resource_count"]
	is.Equal(locked_resource_count, map[string]int{
		"event":      0,
		"group":      0,
		"user":       0,
		"user_login": 0,
	})
}<commit_msg>admin_handlers_test.go: refactor to avoid duplicate codes<commit_after>package api_v1

import (
	"testing"

	"scal/settings"
	. "scal/user_lib"

	"github.com/golang/mock/gomock"
	"github.com/ilius/is"
	. "github.com/ilius/ripo"
)

var origAdminEmails = settings.ADMIN_EMAILS

func resetAdminEmails() {
	settings.ADMIN_EMAILS = origAdminEmails
}

func testAdmin_Unauthenticated(t *testing.T, handler Handler) {
	is := is.New(t)

	ctrl := gomock.NewController(t)
	defer ctrl.Finish()
	mockReq := NewMockExtendedRequest(ctrl)
	var req ExtendedRequest = mockReq

	mockReq.EXPECT().Header(gomock.Eq("Authorization"))

	res, err := handler(req)
	rpcErr := err.(RPCError)
	is.Err(rpcErr)
	is.Equal(rpcErr.Code(), Unauthenticated)
	is.Nil(res)
}

func testAdmin_NotAdmin(t *testing.T, handler Handler) {
	defer testUserMapClear()

	authorization := "abcd"
	email := "<EMAIL>"
	testUserMap = map[string]*UserModel{
		authorization: &UserModel{
			Email: email,
		},
	}

	ctrl := gomock.NewController(t)
	defer ctrl.Finish()
	mockReq := NewMockExtendedRequest(ctrl)
	var req ExtendedRequest = mockReq
	mockReq.EXPECT().Header(gomock.Eq("Authorization")).Return(authorization)

	is := is.New(t)
	res, err := handler(req)
	rpcErr := err.(RPCError)
	is.Err(rpcErr)
	is.Equal(rpcErr.Code(), PermissionDenied)
	is.Equal(rpcErr.Message(), "you are not admin")
	is.Nil(res)
}

func testAdmin_EmailNotConfirmed(t *testing.T, handler Handler) {
	defer testUserMapClear()
	defer resetAdminEmails()

	authorization := "abcd"
	email := "<EMAIL>"
	testUserMap = map[string]*UserModel{
		authorization: &UserModel{
			Email: email,
		},
	}
	settings.ADMIN_EMAILS = []string{email}

	ctrl := gomock.NewController(t)
	defer ctrl.Finish()
	mockReq := NewMockExtendedRequest(ctrl)
	var req ExtendedRequest = mockReq
	mockReq.EXPECT().Header(gomock.Eq("Authorization")).Return(authorization)

	is := is.New(t)
	res, err := handler(req)
	rpcErr := err.(RPCError)
	is.Equal(rpcErr.Code(), PermissionDenied)
	is.Equal(rpcErr.Message(), "email is not confirmed")
	is.Err(rpcErr)
	is.Nil(res)
}

func testAdmin_OK(t *testing.T, handler Handler) *Response {
	defer testUserMapClear()
	defer resetAdminEmails()

	authorization := "abcd"
	email := "<EMAIL>"
	testUserMap = map[string]*UserModel{
		authorization: &UserModel{
			Email:          email,
			EmailConfirmed: true,
		},
	}
	settings.ADMIN_EMAILS = []string{email}

	ctrl := gomock.NewController(t)
	defer ctrl.Finish()
	mockReq := NewMockExtendedRequest(ctrl)
	var req ExtendedRequest = mockReq
	mockReq.EXPECT().Header(gomock.Eq("Authorization")).Return(authorization)

	is := is.New(t)
	res, err := handler(req)
	is.NotErr(err)
	is.NotNil(res)

	return res
}

func TestAdminGetStats(t *testing.T) {
	handler := AdminGetStats

	testAdmin_Unauthenticated(t, handler)
	testAdmin_NotAdmin(t, handler)
	testAdmin_EmailNotConfirmed(t, handler)

	res := testAdmin_OK(t, handler)

	is := is.New(t)
	is.NotNil(res.Data)
	dataMap := res.Data.(map[string]interface{})
	locked_resource_count := dataMap["locked_resource_count"]
	is.Equal(locked_resource_count, map[string]int{
		"event":      0,
		"group":      0,
		"user":       0,
		"user_login": 0,
	})
}
{'fraction_non_alphanumeric': 0.14588859416445624, 'fraction_numerical': 0.0016578249336870027, 'mean_word_length': 5.0877901109989905, 'pattern_counts': {'":': 8, '<': 9, '<?xml version=': 0, '>': 9, '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': '5994784', 'n_tokens_mistral': 2310, 'n_tokens_neox': 2213, 'n_words': 398}