PageRenderTime 41ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/gingae_test.go

https://github.com/frankbille/gingae
Go | 167 lines | 125 code | 42 blank | 0 comment | 4 complexity | a8065a5741c8e1243737732a880e2eb6 MD5 | raw file
Possible License(s): Apache-2.0
  1. package gingae
  2. import (
  3. "appengine"
  4. "appengine_internal"
  5. pb "appengine_internal/user"
  6. "errors"
  7. "github.com/gin-gonic/gin"
  8. . "github.com/smartystreets/goconvey/convey"
  9. "log"
  10. "net/http"
  11. "testing"
  12. )
  13. type MockGaeContext struct {
  14. FailOnCall bool
  15. }
  16. func (c *MockGaeContext) Debugf(format string, args ...interface{}) {
  17. log.Printf(format, args)
  18. }
  19. func (c *MockGaeContext) Infof(format string, args ...interface{}) {
  20. log.Printf(format, args)
  21. }
  22. func (c *MockGaeContext) Warningf(format string, args ...interface{}) {
  23. log.Printf(format, args)
  24. }
  25. func (c *MockGaeContext) Errorf(format string, args ...interface{}) {
  26. log.Printf(format, args)
  27. }
  28. func (c *MockGaeContext) Criticalf(format string, args ...interface{}) {
  29. log.Printf(format, args)
  30. }
  31. func (c *MockGaeContext) Call(service, method string, in, out appengine_internal.ProtoMessage, opts *appengine_internal.CallOptions) error {
  32. if c.FailOnCall {
  33. return errors.New("Failing")
  34. }
  35. if service == "user" {
  36. email := "test@example.com"
  37. authDomain := "example.com"
  38. userId := "userid"
  39. isAdmin := false
  40. out.(*pb.GetOAuthUserResponse).Email = &email
  41. out.(*pb.GetOAuthUserResponse).AuthDomain = &authDomain
  42. out.(*pb.GetOAuthUserResponse).UserId = &userId
  43. out.(*pb.GetOAuthUserResponse).IsAdmin = &isAdmin
  44. } else {
  45. log.Printf("Service: %v", service)
  46. }
  47. return nil
  48. }
  49. func (c *MockGaeContext) FullyQualifiedAppID() string {
  50. return ""
  51. }
  52. func (c *MockGaeContext) Request() interface{} {
  53. header := http.Header{}
  54. header.Add("X-AppEngine-User-Email", "test@example.com")
  55. header.Add("X-AppEngine-User-Federated-Identity", "test@example.com")
  56. return &http.Request{
  57. Header: header,
  58. }
  59. }
  60. func TestGaeContext(t *testing.T) {
  61. Convey("When using the GaeContext middleware", t, func() {
  62. gaeCtx := &MockGaeContext{}
  63. handler := gaeContextFromProvider(func(c *gin.Context) appengine.Context {
  64. return gaeCtx
  65. })
  66. ginCtx := gin.Context{}
  67. handler(&ginCtx)
  68. Convey("The GAE Context should be set on the Gin Context", func() {
  69. foundGaeCtx, getErr := ginCtx.Get(Context)
  70. So(getErr, ShouldBeNil)
  71. So(foundGaeCtx, ShouldEqual, gaeCtx)
  72. })
  73. })
  74. }
  75. func TestGaeUser(t *testing.T) {
  76. Convey("When using the GaeUser middleware", t, func() {
  77. gaeCtx := &MockGaeContext{}
  78. ginCtx := gin.Context{}
  79. Convey("Panic if GaeContext middleware isn't added before.", func() {
  80. userFunc := func() {
  81. GaeUser()(&ginCtx)
  82. }
  83. So(userFunc, ShouldPanic)
  84. })
  85. Convey("The GAE User should be set on the Gin Context", func() {
  86. ginCtx.Set(Context, gaeCtx)
  87. GaeUser()(&ginCtx)
  88. user, getErr := ginCtx.Get(User)
  89. So(getErr, ShouldBeNil)
  90. So(user, ShouldNotBeNil)
  91. })
  92. })
  93. }
  94. func TestGaeUserOAuth(t *testing.T) {
  95. Convey("When using the GaeUserOAuth middleware", t, func() {
  96. gaeCtx := &MockGaeContext{}
  97. ginCtx := gin.Context{}
  98. Convey("Panic if GaeContext middleware isn't added before.", func() {
  99. userFunc := func() {
  100. GaeUserOAuth("")(&ginCtx)
  101. }
  102. So(userFunc, ShouldPanic)
  103. })
  104. Convey("The GAE User should be set on the Gin Context", func() {
  105. ginCtx.Set(Context, gaeCtx)
  106. GaeUserOAuth("")(&ginCtx)
  107. user, getErr := ginCtx.Get(User)
  108. So(getErr, ShouldBeNil)
  109. So(user, ShouldNotBeNil)
  110. })
  111. Convey("If user lookup fails", func() {
  112. gaeCtx = &MockGaeContext{
  113. FailOnCall: true,
  114. }
  115. ginCtx = gin.Context{}
  116. ginCtx.Set(Context, gaeCtx)
  117. GaeUserOAuth("")(&ginCtx)
  118. Convey("User should not be set on Gin Context, but User error should", func() {
  119. user, getErr := ginCtx.Get(User)
  120. So(getErr, ShouldNotBeNil)
  121. So(user, ShouldBeNil)
  122. })
  123. })
  124. })
  125. }