register_test.go 2.89 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
package tests

import (
	"io/ioutil"
	"os"
	"os/exec"
	"strings"
	"testing"
	"time"

	"github.com/bmizerany/assert"
)

// TestRegisterAuth tries to register and auth several users.
//
// GOOD CASES
// client1 : test@example.com with password
// client2 : test@example.com without password
//
// BAD CASES
// client3 : test@example.com
// client4 : wrong mail
// client5 : wrong key size
// client6 : wrong mail during auth
// client7 : wrong token during auth
26 27 28
//
// TODO Add expired accounts test
// TODO Add Stderr test
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
func TestRegisterAuth(t *testing.T) {
	// Cleanup
	eraseDatabase()

	// Start the platform
	workingDir, err := ioutil.TempDir("", "dfss_")
	assert.Equal(t, nil, err)
	platform, ca, err := startPlatform(workingDir)
	assert.Equal(t, nil, err)
	defer func() {
		_ = platform.Process.Kill()
		_ = os.RemoveAll(workingDir)
	}()

	time.Sleep(2 * time.Second)

	// Register client1
	client1, err := createClient(workingDir, ca)
	assert.Equal(t, nil, err)
	err = registerAndAuth(client1, "test@example.com", "password", "", true, true)
	assert.Equal(t, nil, err)

	// Register client2
	client2, err := createClient(workingDir, ca)
	assert.Equal(t, nil, err)
	err = registerAndAuth(client2, "test2@example.com", "", "2048", true, true)
	assert.Equal(t, nil, err)

	// Register client3
	client3, err := createClient(workingDir, ca)
	assert.Equal(t, nil, err)
	err = registerAndAuth(client3, "test@example.com", "", "", true, true)
	assert.NotEqual(t, nil, err)

	// Register client4
	client4, err := createClient(workingDir, ca)
	assert.Equal(t, nil, err)
	err = registerAndAuth(client4, "test wrong mail", "", "", true, true)
	assert.NotEqual(t, nil, err)

	// Register client5
	client5, err := createClient(workingDir, ca)
	assert.Equal(t, nil, err)
	err = registerAndAuth(client5, "wrong@key.fr", "", "1024", true, true)
	assert.NotEqual(t, nil, err)

	// Register client6
	client6, err := createClient(workingDir, ca)
	assert.Equal(t, nil, err)
	err = registerAndAuth(client6, "bad@auth.com", "", "", false, true)
	assert.NotEqual(t, nil, err)

	// Register client7
	client7, err := createClient(workingDir, ca)
	assert.Equal(t, nil, err)
	err = registerAndAuth(client7, "bad@auth2.com", "", "", true, false)
	assert.NotEqual(t, nil, err)
}

func registerAndAuth(client *exec.Cmd, mail, password, keySize string, authMail, authToken bool) error {

	setLastArg(client, "register", false)
	client.Stdin = strings.NewReader(
		mail + "\n" +
			"FR\n" +
			"TEST\n" +
			"TEST\n" +
			keySize + "\n" +
			password + "\n" +
			password + "\n",
	)
	err := client.Run()
	if err != nil {
		return err
	}

	if !authMail { // simulates wrong mail
		mail = "very@badmail.com"
	}

	token := "badToken" // simulates wrong token
	if authToken {
		token = getRegistrationToken(mail)
	}

	// Auth client
	client = newClient(client)
	setLastArg(client, "auth", true)
	client.Stdin = strings.NewReader(
		mail + "\n" +
			token + "\n",
	)
	return client.Run()
}