server_test.go 2.38 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package sessiontest

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

	"github.com/Sirupsen/logrus"
	"github.com/privacybydesign/irmago"
	"github.com/privacybydesign/irmago/internal/test"
	"github.com/privacybydesign/irmago/irmaserver"
	"github.com/privacybydesign/irmago/irmaserver/server"
	"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() {
25
26
27
28
29
30
31
		err := server.Start(&server.Configuration{
			Configuration: &irmaserver.Configuration{
				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() {
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
	server.Stop()
}

func serverSessionHelper(t *testing.T, request irma.SessionRequest) *irmaserver.SessionResult {
	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)
	}

	var result irmaserver.SessionResult
	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"])
}