server_test.go 3.46 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"
)

Sietse Ringers's avatar
Sietse Ringers committed
17
func StartIrmaServer() {
18
	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
				Logger:                logger,
				IrmaConfigurationPath: filepath.Join(testdata, "irma_configuration"),
29
				IssuerPrivateKeysPath: filepath.Join(testdata, "privatekeys"),
30
31
			},
			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
}

Sietse Ringers's avatar
Sietse Ringers committed
40
func StopIrmaServer() {
Sietse Ringers's avatar
Sietse Ringers committed
41
	irmaserver.Stop()
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
func StartIrmaJwtServer() {
	testdata := test.FindTestdataFolder(nil)

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

	go func() {
		err := irmaserver.Start(&irmaserver.Configuration{
			Configuration: &server.Configuration{
				Logger:                logger,
				IrmaConfigurationPath: filepath.Join(testdata, "irma_configuration"),
				IssuerPrivateKeysPath: filepath.Join(testdata, "privatekeys"),
			},
			Port: 48682,
			AuthenticateRequestors: true,
			GlobalPermissions: irmaserver.Permissions{
				Disclosing: []string{"*"},
				Signing:    []string{"*"},
				Issuing:    []string{"*"},
			},
			Requestors: map[string]irmaserver.Requestor{
				"testrequestor": irmaserver.Requestor{
					AuthenticationMethod: irmaserver.AuthenticationMethodPublicKey,
					AuthenticationKey:    filepath.Join(testdata, "jwtkeys", "testrequestor.pem"),
				},
			},
			PrivateKey: filepath.Join(testdata, "jwtkeys", "sk.pem"),
		})
		if err != nil {
			panic("Starting server failed: " + err.Error())
		}
	}()
	time.Sleep(100 * time.Millisecond) // Give server time to start
}

Sietse Ringers's avatar
Sietse Ringers committed
80
func serverSessionHelper(t *testing.T, request irma.SessionRequest) *server.SessionResult {
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
	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
103
	var result server.SessionResult
104
105
106
107
	transport.Get("result/"+token, &result)
	return &result
}

Sietse Ringers's avatar
Sietse Ringers committed
108
func TestIrmaServer(t *testing.T) {
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
	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"])
}