server_test.go 2.37 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
package sessiontest

import (
	"encoding/json"
	"path/filepath"
	"testing"
	"time"

	"github.com/Sirupsen/logrus"
	"github.com/privacybydesign/irmago"
	"github.com/privacybydesign/irmago/internal/test"
Sietse Ringers's avatar
Sietse Ringers committed
12
13
	"github.com/privacybydesign/irmago/server"
	"github.com/privacybydesign/irmago/server/irmaserver"
14
15
16
	"github.com/stretchr/testify/require"
)

17
18
func StartRequestorServer() {
	testdata := test.FindTestdataFolder(nil)
19
20
21
22
23
24

	logger := logrus.New()
	logger.Level = logrus.WarnLevel
	logger.Formatter = &logrus.TextFormatter{}

	go func() {
Sietse Ringers's avatar
Sietse Ringers committed
25
26
		err := irmaserver.Start(&irmaserver.Configuration{
			Configuration: &server.Configuration{
27
28
29
30
31
				Logger:                logger,
				IrmaConfigurationPath: filepath.Join(testdata, "irma_configuration"),
				PrivateKeysPath:       filepath.Join(testdata, "privatekeys"),
			},
			Port: 48682,
32
		})
33
34
35
		if err != nil {
			panic("Starting server failed: " + err.Error())
		}
36
	}()
37
	time.Sleep(100 * time.Millisecond) // Give server time to start
38
39
}

40
func StopRequestorServer() {
Sietse Ringers's avatar
Sietse Ringers committed
41
	irmaserver.Stop()
42
43
}

Sietse Ringers's avatar
Sietse Ringers committed
44
func serverSessionHelper(t *testing.T, request irma.SessionRequest) *server.SessionResult {
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
	client := parseStorage(t)
	defer test.ClearTestStorage(t)

	clientChan := make(chan *SessionResult)

	transport := irma.NewHTTPTransport("http://localhost:48682")
	var qr irma.Qr
	err := transport.Post("create", &qr, request)
	require.NoError(t, err)

	token := qr.URL
	qr.URL = "http://localhost:48682/irma/" + qr.URL

	h := TestHandler{t, clientChan, client}
	j, err := json.Marshal(qr)
	require.NoError(t, err)
	client.NewSession(string(j), h)
	clientResult := <-clientChan
	if clientResult != nil {
		require.NoError(t, clientResult.Err)
	}

Sietse Ringers's avatar
Sietse Ringers committed
67
	var result server.SessionResult
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
	transport.Get("result/"+token, &result)
	return &result
}

func TestRequestorServer(t *testing.T) {
	id := irma.NewAttributeTypeIdentifier("irma-demo.RU.studentCard.studentID")
	serverResult := serverSessionHelper(t, &irma.DisclosureRequest{
		BaseRequest: irma.BaseRequest{Type: irma.ActionDisclosing},
		Content: irma.AttributeDisjunctionList([]*irma.AttributeDisjunction{{
			Label:      "foo",
			Attributes: []irma.AttributeTypeIdentifier{id},
		}}),
	})

	require.Nil(t, serverResult.Err)
	require.Equal(t, irma.ProofStatusValid, serverResult.ProofStatus)
	require.NotEmpty(t, serverResult.Disclosed)
	require.Equal(t, id, serverResult.Disclosed[0].Identifier)
	require.Equal(t, "456", serverResult.Disclosed[0].Value["en"])
}