<commit_before>// This file is part of Monsti, a web content management system.
// Copyright 2012-2013 <NAME>
// 
// Monsti is free software: you can redistribute it and/or modify it under the
// terms of the GNU Affero General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option) any
// later version.
//
// Monsti is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
// A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
// details.
//
// You should have received a copy of the GNU Affero General Public License
// along with Monsti.  If not, see <http://www.gnu.org/licenses/>.

package main

import (
	"code.google.com/p/go.crypto/bcrypt"
	"pkg.monsti.org/rpc/client"
	"io/ioutil"
	"path/filepath"
	"reflect"
	"testing"
)

func TestCheckPermission(t *testing.T) {
	tests := []struct {
		Action      string
		Auth, Grant bool
	}{
		{"", false, true},
		{"", true, true},
		{"login", false, true},
		{"login", true, true},
		{"logout", false, false},
		{"logout", true, true},
		{"edit", false, false},
		{"edit", true, true},
		{"add", false, false},
		{"add", true, true},
		{"remove", false, false},
		{"remove", true, true},
		{"unknown_action", true, false},
		{"unknown_action", false, false}}
	for _, v := range tests {
		var user *client.User
		if v.Auth {
			user = &client.User{}
		}
		ret := checkPermission(v.Action, &client.Session{User: user})
		if ret != v.Grant {
			t.Errorf("checkPermission(%v, %v) = %v, expected %v", v.Action,
				user, ret, v.Grant)
		}
	}
}

func TestGetUser(t *testing.T) {
	root, err := ioutil.TempDir("", "_monsti_get_user")
	if err != nil {
		t.Fatalf("Could not create temp dir: %s", err)
	}
	db := []byte(`- login: foo
  name: Mr. Foo
  password: <PASSWORD>
  email: <EMAIL>
- login: bar
  name: Mrs. Bar
  email: <EMAIL>
  password: <PASSWORD>
`)
	if err = ioutil.WriteFile(filepath.Join(root, "users.yaml"),
		db, 0600); err != nil {
		t.Fatalf("Could not write navigation: %s", err)
	}
	tests := []struct {
		Login string
		User  *client.User
	}{
		{Login: "unknown", User: nil},
		{Login: "foo", User: &client.User{Login: "foo", Password: "<PASSWORD>",
			Name: "Mr. Foo", Email: "<EMAIL>"}},
		{Login: "bar", User: &client.User{Login: "bar", Password: "<PASSWORD>",
			Name: "Mrs. Bar", Email: "<EMAIL>"}}}
	for _, v := range tests {
		user := getUser(v.Login, root)
		if !reflect.DeepEqual(user, v.User) {
			t.Errorf("getUser(%q, _) = %v, should be %v", v.Login,
				user, v.User)
		}
	}
}

func TestPasswordEqual(t *testing.T) {
	tests := []struct {
		ToHash, Password string
		Equal            bool
	}{
		{"foobar", "foobar", true},<commit_msg>Fix test.<commit_after>// This file is part of Monsti, a web content management system.
// Copyright 2012-2013 <NAME>
// 
// Monsti is free software: you can redistribute it and/or modify it under the
// terms of the GNU Affero General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option) any
// later version.
//
// Monsti is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
// A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
// details.
//
// You should have received a copy of the GNU Affero General Public License
// along with Monsti.  If not, see <http://www.gnu.org/licenses/>.

package main

import (
	"code.google.com/p/go.crypto/bcrypt"
	"pkg.monsti.org/service"
	"io/ioutil"
	"path/filepath"
	"reflect"
	"testing"
)

func TestCheckPermission(t *testing.T) {
	tests := []struct {
		Action      string
		Auth, Grant bool
	}{
		{"", false, true},
		{"", true, true},
		{"login", false, true},
		{"login", true, true},
		{"logout", false, false},
		{"logout", true, true},
		{"edit", false, false},
		{"edit", true, true},
		{"add", false, false},
		{"add", true, true},
		{"remove", false, false},
		{"remove", true, true},
		{"unknown_action", true, false},
		{"unknown_action", false, false}}
	for _, v := range tests {
		var user *service.User
		if v.Auth {
			user = &service.User{}
		}
		ret := checkPermission(v.Action, &service.UserSession{User: user})
		if ret != v.Grant {
			t.Errorf("checkPermission(%v, %v) = %v, expected %v", v.Action,
				user, ret, v.Grant)
		}
	}
}

func TestGetUser(t *testing.T) {
	root, err := ioutil.TempDir("", "_monsti_get_user")
	if err != nil {
		t.Fatalf("Could not create temp dir: %s", err)
	}
	db := []byte(`- login: foo
  name: <NAME>
  password: <PASSWORD>
  email: <EMAIL>
- login: bar
  name: <NAME>
  email: <EMAIL>
  password: <PASSWORD>
`)
	if err = ioutil.WriteFile(filepath.Join(root, "users.yaml"),
		db, 0600); err != nil {
		t.Fatalf("Could not write navigation: %s", err)
	}
	tests := []struct {
		Login string
		User  *service.User
	}{
		{Login: "unknown", User: nil},
		{Login: "foo", User: &service.User{Login: "foo", Password: "<PASSWORD>",
			Name: "<NAME>", Email: "<EMAIL>"}},
		{Login: "bar", User: &service.User{Login: "bar", Password: "<PASSWORD>",
			Name: "<NAME>", Email: "<EMAIL>"}}}
	for _, v := range tests {
		user := getUser(v.Login, root)
		if !reflect.DeepEqual(user, v.User) {
			t.Errorf("getUser(%q, _) = %v, should be %v", v.Login,
				user, v.User)
		}
	}
}

func TestPasswordEqual(t *testing.T) {
	tests := []struct {
		ToHash, Password string
		Equal            bool
	}{
		{"foobar", "foobar", true},
{'fraction_non_alphanumeric': 0.17083258406761373, 'fraction_numerical': 0.004675418090271534, 'mean_word_length': 3.492730210016155, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7171732', 'n_tokens_mistral': 2074, 'n_tokens_neox': 1920, 'n_words': 628}