You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

oauth2_test.go 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Copyright 2014 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package oauth2
  15. import (
  16. "net/http"
  17. "net/http/httptest"
  18. "testing"
  19. "github.com/go-martini/martini"
  20. "github.com/martini-contrib/sessions"
  21. )
  22. func Test_LoginRedirect(t *testing.T) {
  23. recorder := httptest.NewRecorder()
  24. m := martini.New()
  25. m.Use(sessions.Sessions("my_session", sessions.NewCookieStore([]byte("secret123"))))
  26. m.Use(Google(&Options{
  27. ClientId: "client_id",
  28. ClientSecret: "client_secret",
  29. RedirectURL: "refresh_url",
  30. Scopes: []string{"x", "y"},
  31. }))
  32. r, _ := http.NewRequest("GET", "/login", nil)
  33. m.ServeHTTP(recorder, r)
  34. location := recorder.HeaderMap["Location"][0]
  35. if recorder.Code != 302 {
  36. t.Errorf("Not being redirected to the auth page.")
  37. }
  38. if location != "https://accounts.google.com/o/oauth2/auth?access_type=&approval_prompt=&client_id=client_id&redirect_uri=refresh_url&response_type=code&scope=x+y&state=" {
  39. t.Errorf("Not being redirected to the right page, %v found", location)
  40. }
  41. }
  42. func Test_LoginRedirectAfterLoginRequired(t *testing.T) {
  43. recorder := httptest.NewRecorder()
  44. m := martini.Classic()
  45. m.Use(sessions.Sessions("my_session", sessions.NewCookieStore([]byte("secret123"))))
  46. m.Use(Google(&Options{
  47. ClientId: "client_id",
  48. ClientSecret: "client_secret",
  49. RedirectURL: "refresh_url",
  50. Scopes: []string{"x", "y"},
  51. }))
  52. m.Get("/login-required", LoginRequired, func(tokens Tokens) (int, string) {
  53. return 200, tokens.Access()
  54. })
  55. r, _ := http.NewRequest("GET", "/login-required?key=value", nil)
  56. m.ServeHTTP(recorder, r)
  57. location := recorder.HeaderMap["Location"][0]
  58. if recorder.Code != 302 {
  59. t.Errorf("Not being redirected to the auth page.")
  60. }
  61. if location != "/login?next=%2Flogin-required%3Fkey%3Dvalue" {
  62. t.Errorf("Not being redirected to the right page, %v found", location)
  63. }
  64. }
  65. func Test_Logout(t *testing.T) {
  66. recorder := httptest.NewRecorder()
  67. s := sessions.NewCookieStore([]byte("secret123"))
  68. m := martini.Classic()
  69. m.Use(sessions.Sessions("my_session", s))
  70. m.Use(Google(&Options{
  71. // no need to configure
  72. }))
  73. m.Get("/", func(s sessions.Session) {
  74. s.Set(keyToken, "dummy token")
  75. })
  76. m.Get("/get", func(s sessions.Session) {
  77. if s.Get(keyToken) != nil {
  78. t.Errorf("User credentials are still kept in the session.")
  79. }
  80. })
  81. logout, _ := http.NewRequest("GET", "/logout", nil)
  82. index, _ := http.NewRequest("GET", "/", nil)
  83. m.ServeHTTP(httptest.NewRecorder(), index)
  84. m.ServeHTTP(recorder, logout)
  85. if recorder.Code != 302 {
  86. t.Errorf("Not being redirected to the next page.")
  87. }
  88. }
  89. func Test_LogoutOnAccessTokenExpiration(t *testing.T) {
  90. recorder := httptest.NewRecorder()
  91. s := sessions.NewCookieStore([]byte("secret123"))
  92. m := martini.Classic()
  93. m.Use(sessions.Sessions("my_session", s))
  94. m.Use(Google(&Options{
  95. // no need to configure
  96. }))
  97. m.Get("/addtoken", func(s sessions.Session) {
  98. s.Set(keyToken, "dummy token")
  99. })
  100. m.Get("/", func(s sessions.Session) {
  101. if s.Get(keyToken) != nil {
  102. t.Errorf("User not logged out although access token is expired.")
  103. }
  104. })
  105. addtoken, _ := http.NewRequest("GET", "/addtoken", nil)
  106. index, _ := http.NewRequest("GET", "/", nil)
  107. m.ServeHTTP(recorder, addtoken)
  108. m.ServeHTTP(recorder, index)
  109. }
  110. func Test_InjectedTokens(t *testing.T) {
  111. recorder := httptest.NewRecorder()
  112. m := martini.Classic()
  113. m.Use(sessions.Sessions("my_session", sessions.NewCookieStore([]byte("secret123"))))
  114. m.Use(Google(&Options{
  115. // no need to configure
  116. }))
  117. m.Get("/", func(tokens Tokens) string {
  118. return "Hello world!"
  119. })
  120. r, _ := http.NewRequest("GET", "/", nil)
  121. m.ServeHTTP(recorder, r)
  122. }
  123. func Test_LoginRequired(t *testing.T) {
  124. recorder := httptest.NewRecorder()
  125. m := martini.Classic()
  126. m.Use(sessions.Sessions("my_session", sessions.NewCookieStore([]byte("secret123"))))
  127. m.Use(Google(&Options{
  128. // no need to configure
  129. }))
  130. m.Get("/", LoginRequired, func(tokens Tokens) string {
  131. return "Hello world!"
  132. })
  133. r, _ := http.NewRequest("GET", "/", nil)
  134. m.ServeHTTP(recorder, r)
  135. if recorder.Code != 302 {
  136. t.Errorf("Not being redirected to the auth page although user is not logged in.")
  137. }
  138. }