Skip to content
Snippets Groups Projects
Commit dae88815 authored by Loïck Bonniot's avatar Loïck Bonniot
Browse files

[tests] Add scenario for user registration

parent 8a0a5a5e
No related branches found
No related tags found
1 merge request!32186 integration tests
......@@ -38,26 +38,16 @@ Unit tests:
- "grep -h -v 'mode: set' *part >> c.out"
- "go tool cover -html=c.out -o coverage.html"
ARM tests:
Integration tests:
stage: test
tags:
- arm
- golang
- mongo
script:
- "ln -s -f $(pwd) $GOPATH/src/dfss"
- "./build/deps.sh"
- "cd $GOPATH/src/dfss && go install ./..."
- "go test -cover -short -v dfss/auth"
- "go test -cover -short -v dfss/mgdb"
- "go test -cover -short -v dfss/net"
- "go test -cover -short -v dfss/dfssp/authority"
- "go test -cover -short -v dfss/dfssp/user"
- "go test -cover -short -v dfss/dfssp/contract"
- "go test -cover -short -v dfss/dfssp/templates"
- "go test -cover -short -v dfss/dfssd"
- "go test -cover -short -v dfss/dfssc/common"
- "go test -cover -short -v dfss/dfssc/security"
- "go test -cover -short -v dfss/dfssc/user"
- "go test -cover -short -v dfss/dfssc/sign"
- "go test -v dfss/tests"
Code lint:
stage: test
......
......@@ -2,6 +2,7 @@ package main
import (
"fmt"
"os"
"dfss/dfssc/user"
)
......@@ -16,5 +17,6 @@ func authUser() {
err := user.Authenticate(fca, fcert, addrPort, mail, token)
if err != nil {
fmt.Println("An error occurred : ", err.Error())
os.Exit(3)
}
}
......@@ -2,6 +2,7 @@ package main
import (
"fmt"
"os"
"dfss/dfssc/sign"
)
......@@ -13,6 +14,7 @@ func newContract() {
err := sign.NewCreateManager(fca, fcert, fkey, addrPort, passphrase, filepath, comment, signers)
if err != nil {
fmt.Println(err)
os.Exit(4)
}
}
......
......@@ -35,6 +35,7 @@ func registerUser() {
err = readPassword(&passphrase, true)
if err != nil {
fmt.Println("An error occurred:", err.Error())
os.Exit(1)
return
}
......@@ -42,9 +43,13 @@ func registerUser() {
err = user.Register(fca, fcert, fkey, addrPort, passphrase, country, organization, unit, mail, bits)
if err != nil {
fmt.Println("An error occurred:", err.Error())
os.Exit(2)
}
}
// We need to use ONLY ONE reader: buffio buffers some data (= consumes from stdin)
var reader *bufio.Reader
// Get a string parameter from standard input
func readStringParam(message, def string, ptr *string) {
fmt.Print(message)
......@@ -53,7 +58,9 @@ func readStringParam(message, def string, ptr *string) {
}
fmt.Print(": ")
reader := bufio.NewReader(os.Stdin)
if reader == nil {
reader = bufio.NewReader(os.Stdin)
}
value, _ := reader.ReadString('\n')
// Trim newline symbols
......
......@@ -42,10 +42,11 @@ func checkRegisterRequest(in *api.RegisterRequest) *api.ErrorCode {
// Send the verification email in response to the specified registration request
//
// This method should only be called AFTER checking the RegisterRequest for validity
func sendVerificationMail(in *api.RegisterRequest, token string) error {
func sendVerificationMail(in *api.RegisterRequest, token string) {
conn := templates.MailConn()
if conn == nil {
return errors.New("Couldn't connect to the dfssp mail server")
log.Println("Couldn't connect to the dfssp mail server")
return
}
defer func() { _ = conn.Close() }()
......@@ -54,7 +55,8 @@ func sendVerificationMail(in *api.RegisterRequest, token string) error {
mail := templates.VerificationMail{Token: token}
content, err := templates.Get("verificationMail", mail)
if err != nil {
return err
log.Println(err)
return
}
err = conn.Send(
......@@ -66,10 +68,9 @@ func sendVerificationMail(in *api.RegisterRequest, token string) error {
nil,
)
if err != nil {
return err
log.Println(err)
return
}
return nil
}
// Register checks if the registration request is valid, and if so,
......@@ -117,10 +118,7 @@ func Register(manager *mgdb.MongoManager, in *api.RegisterRequest) (*api.ErrorCo
}
// Sending the email
err = sendVerificationMail(in, token)
if err != nil {
return &api.ErrorCode{Code: api.ErrorCode_INTERR, Message: "Error during the sending of the email"}, err
}
sendVerificationMail(in, token)
return &api.ErrorCode{Code: api.ErrorCode_SUCCESS, Message: "Registration successful ; email sent"}, nil
}
......
package tests
import (
"fmt"
"os"
"testing"
"dfss/mgdb"
)
func TestMain(m *testing.M) {
dbURI = os.Getenv("DFSS_MONGO_URI")
if dbURI == "" {
dbURI = "mongodb://localhost/dfss-test"
}
var err error
dbManager, err = mgdb.NewManager(dbURI)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
code := m.Run()
eraseDatabase()
os.Exit(code)
}
package tests
import (
"dfss/dfssp/entities"
"dfss/mgdb"
"gopkg.in/mgo.v2/bson"
)
var dbURI string
var dbManager *mgdb.MongoManager
// EraseDatabase drops the test database.
func eraseDatabase() {
_ = dbManager.Database.DropDatabase()
}
func getRegistrationToken(mail string) string {
var user entities.User
_ = dbManager.Get("users").Collection.Find(bson.M{
"email": mail,
}).One(&user)
if len(user.RegToken) == 0 {
return "badToken"
}
return user.RegToken
}
package tests
// A test package, see test files.
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
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()
}
package tests
import (
"io/ioutil"
"os"
"testing"
"time"
"github.com/bmizerany/assert"
)
func TestSimple(t *testing.T) {
// Start the platform
workingDir, err := ioutil.TempDir("", "dfss_")
assert.Equal(t, nil, err)
cmd, ca, err := StartPlatform(workingDir)
assert.Equal(t, nil, err)
time.Sleep(3 * time.Second)
// Start a client
_, err = CreateClient(workingDir, ca)
assert.Equal(t, nil, err)
// Shutdown
err = cmd.Process.Kill()
assert.Equal(t, nil, err)
// Cleanup
_ = os.RemoveAll(workingDir)
}
......@@ -13,12 +13,8 @@ const testPort = "9090"
var currentClient = 0
// StartPlatform creates root certificate for the platform and starts the platform.
func StartPlatform(tmpDir string) (*exec.Cmd, []byte, error) {
func startPlatform(tmpDir string) (*exec.Cmd, []byte, error) {
path := filepath.Join(os.Getenv("GOPATH"), "bin", "dfssp")
db := os.Getenv("DFSS_MONGO_URI")
if db == "" {
db = "mongodb://localhost/dfss"
}
// Create temporary directory for platform
dir, err := ioutil.TempDir(tmpDir, "p_")
......@@ -40,7 +36,7 @@ func StartPlatform(tmpDir string) (*exec.Cmd, []byte, error) {
}
// Start
cmd = exec.Command(path, "-db", db, "-path", dir, "-p", testPort, "-v", "start")
cmd = exec.Command(path, "-db", dbURI, "-path", dir, "-p", testPort, "-v", "start")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Start()
......@@ -49,8 +45,8 @@ func StartPlatform(tmpDir string) (*exec.Cmd, []byte, error) {
}
// CreateClient creates a new working directory for a client, creating ca.pem.
// It returns a ready-to-run command, but you probably want to change the last argument of the command.
func CreateClient(tmpDir string, ca []byte) (*exec.Cmd, error) {
// It returns a ready-to-run command, but you probably want to set the last argument of the command.
func createClient(tmpDir string, ca []byte) (*exec.Cmd, error) {
path := filepath.Join(os.Getenv("GOPATH"), "bin", "dfssc")
// Create temporary directory for client
......@@ -73,7 +69,26 @@ func CreateClient(tmpDir string, ca []byte) (*exec.Cmd, error) {
// Prepare the client command.
// The last argument is up to you!
cmd := exec.Command(path, "-ca", caPath, "-cert", certPath, "-host", "localhost:"+testPort, "-key", keyPath, "-v", "CHANGE_ME")
cmd := exec.Command(path, "-ca", caPath, "-cert", certPath, "-host", "localhost:"+testPort, "-key", keyPath, "-v")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd, nil
}
func newClient(old *exec.Cmd) *exec.Cmd {
cmd := exec.Command(old.Path)
cmd.Args = old.Args
cmd.Stdout = old.Stdout
cmd.Stderr = old.Stderr
return cmd
}
// SetLastArg sets or updates the last argument of a command.
func setLastArg(cmd *exec.Cmd, str string, override bool) {
if override {
cmd.Args = cmd.Args[:(len(cmd.Args) - 1)]
}
cmd.Args = append(cmd.Args, str)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment