user_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright 2011 Google Inc. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. // +build !appengine
  5. package user
  6. import (
  7. "fmt"
  8. "net/http"
  9. "testing"
  10. "github.com/golang/protobuf/proto"
  11. "google.golang.org/appengine/internal"
  12. "google.golang.org/appengine/internal/aetesting"
  13. pb "google.golang.org/appengine/internal/user"
  14. )
  15. func baseReq() *http.Request {
  16. return &http.Request{
  17. Header: http.Header{},
  18. }
  19. }
  20. type basicUserTest struct {
  21. nickname, email, authDomain, admin string
  22. // expectations
  23. isNil, isAdmin bool
  24. displayName string
  25. }
  26. var basicUserTests = []basicUserTest{
  27. {"", "", "", "0", true, false, ""},
  28. {"ken", "ken@example.com", "example.com", "0", false, false, "ken"},
  29. {"ken", "ken@example.com", "auth_domain.com", "1", false, true, "ken@example.com"},
  30. }
  31. func TestBasicUserAPI(t *testing.T) {
  32. for i, tc := range basicUserTests {
  33. req := baseReq()
  34. req.Header.Set("X-AppEngine-User-Nickname", tc.nickname)
  35. req.Header.Set("X-AppEngine-User-Email", tc.email)
  36. req.Header.Set("X-AppEngine-Auth-Domain", tc.authDomain)
  37. req.Header.Set("X-AppEngine-User-Is-Admin", tc.admin)
  38. c := internal.ContextForTesting(req)
  39. if ga := IsAdmin(c); ga != tc.isAdmin {
  40. t.Errorf("test %d: expected IsAdmin(c) = %v, got %v", i, tc.isAdmin, ga)
  41. }
  42. u := Current(c)
  43. if tc.isNil {
  44. if u != nil {
  45. t.Errorf("test %d: expected u == nil, got %+v", i, u)
  46. }
  47. continue
  48. }
  49. if u == nil {
  50. t.Errorf("test %d: expected u != nil, got nil", i)
  51. continue
  52. }
  53. if u.Email != tc.email {
  54. t.Errorf("test %d: expected u.Email = %q, got %q", i, tc.email, u.Email)
  55. }
  56. if gs := u.String(); gs != tc.displayName {
  57. t.Errorf("test %d: expected u.String() = %q, got %q", i, tc.displayName, gs)
  58. }
  59. if u.Admin != tc.isAdmin {
  60. t.Errorf("test %d: expected u.Admin = %v, got %v", i, tc.isAdmin, u.Admin)
  61. }
  62. }
  63. }
  64. func TestLoginURL(t *testing.T) {
  65. expectedQuery := &pb.CreateLoginURLRequest{
  66. DestinationUrl: proto.String("/destination"),
  67. }
  68. const expectedDest = "/redir/dest"
  69. c := aetesting.FakeSingleContext(t, "user", "CreateLoginURL", func(req *pb.CreateLoginURLRequest, res *pb.CreateLoginURLResponse) error {
  70. if !proto.Equal(req, expectedQuery) {
  71. return fmt.Errorf("got %v, want %v", req, expectedQuery)
  72. }
  73. res.LoginUrl = proto.String(expectedDest)
  74. return nil
  75. })
  76. url, err := LoginURL(c, "/destination")
  77. if err != nil {
  78. t.Fatalf("LoginURL failed: %v", err)
  79. }
  80. if url != expectedDest {
  81. t.Errorf("got %v, want %v", url, expectedDest)
  82. }
  83. }
  84. // TODO(dsymonds): Add test for LogoutURL.