aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/google.golang.org/appengine/user/user_test.go
blob: 5fc5957a8cec040ddd7ef9f20a698a9e3c807eda (plain)
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
26
27
28
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
// Copyright 2011 Google Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.

// +build !appengine

package user

import (
	"fmt"
	"net/http"
	"testing"

	"github.com/golang/protobuf/proto"

	"google.golang.org/appengine/internal"
	"google.golang.org/appengine/internal/aetesting"
	pb "google.golang.org/appengine/internal/user"
)

func baseReq() *http.Request {
	return &http.Request{
		Header: http.Header{},
	}
}

type basicUserTest struct {
	nickname, email, authDomain, admin string
	// expectations
	isNil, isAdmin bool
	displayName    string
}

var basicUserTests = []basicUserTest{
	{"", "", "", "0", true, false, ""},
	{"ken", "ken@example.com", "example.com", "0", false, false, "ken"},
	{"ken", "ken@example.com", "auth_domain.com", "1", false, true, "ken@example.com"},
}

func TestBasicUserAPI(t *testing.T) {
	for i, tc := range basicUserTests {
		req := baseReq()
		req.Header.Set("X-AppEngine-User-Nickname", tc.nickname)
		req.Header.Set("X-AppEngine-User-Email", tc.email)
		req.Header.Set("X-AppEngine-Auth-Domain", tc.authDomain)
		req.Header.Set("X-AppEngine-User-Is-Admin", tc.admin)

		c := internal.ContextForTesting(req)

		if ga := IsAdmin(c); ga != tc.isAdmin {
			t.Errorf("test %d: expected IsAdmin(c) = %v, got %v", i, tc.isAdmin, ga)
		}

		u := Current(c)
		if tc.isNil {
			if u != nil {
				t.Errorf("test %d: expected u == nil, got %+v", i, u)
			}
			continue
		}
		if u == nil {
			t.Errorf("test %d: expected u != nil, got nil", i)
			continue
		}
		if u.Email != tc.email {
			t.Errorf("test %d: expected u.Email = %q, got %q", i, tc.email, u.Email)
		}
		if gs := u.String(); gs != tc.displayName {
			t.Errorf("test %d: expected u.String() = %q, got %q", i, tc.displayName, gs)
		}
		if u.Admin != tc.isAdmin {
			t.Errorf("test %d: expected u.Admin = %v, got %v", i, tc.isAdmin, u.Admin)
		}
	}
}

func TestLoginURL(t *testing.T) {
	expectedQuery := &pb.CreateLoginURLRequest{
		DestinationUrl: proto.String("/destination"),
	}
	const expectedDest = "/redir/dest"
	c := aetesting.FakeSingleContext(t, "user", "CreateLoginURL", func(req *pb.CreateLoginURLRequest, res *pb.CreateLoginURLResponse) error {
		if !proto.Equal(req, expectedQuery) {
			return fmt.Errorf("got %v, want %v", req, expectedQuery)
		}
		res.LoginUrl = proto.String(expectedDest)
		return nil
	})

	url, err := LoginURL(c, "/destination")
	if err != nil {
		t.Fatalf("LoginURL failed: %v", err)
	}
	if url != expectedDest {
		t.Errorf("got %v, want %v", url, expectedDest)
	}
}

// TODO(dsymonds): Add test for LogoutURL.