permit_test.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. package permit
  2. import (
  3. "context"
  4. "net/http"
  5. "net/url"
  6. "sync"
  7. "testing"
  8. "time"
  9. "go-common/library/cache/memcache"
  10. "go-common/library/container/pool"
  11. "go-common/library/ecode"
  12. "go-common/library/log"
  13. bm "go-common/library/net/http/blademaster"
  14. "go-common/library/net/netutil/breaker"
  15. xtime "go-common/library/time"
  16. )
  17. var (
  18. once sync.Once
  19. )
  20. type Response struct {
  21. Code int `json:"code"`
  22. Data string `json:"data"`
  23. }
  24. func init() {
  25. log.Init(nil)
  26. }
  27. func client() *bm.Client {
  28. return bm.NewClient(&bm.ClientConfig{
  29. App: &bm.App{
  30. Key: "test",
  31. Secret: "test",
  32. },
  33. Dial: xtime.Duration(time.Second),
  34. Timeout: xtime.Duration(time.Second),
  35. KeepAlive: xtime.Duration(time.Second * 10),
  36. Breaker: &breaker.Config{
  37. Window: xtime.Duration(time.Second),
  38. Sleep: xtime.Duration(time.Millisecond * 100),
  39. Bucket: 10,
  40. Ratio: 0.5,
  41. Request: 100,
  42. },
  43. })
  44. }
  45. func getPermit() *Permit {
  46. return New(&Config{
  47. DsHTTPClient: &bm.ClientConfig{
  48. App: &bm.App{
  49. Key: "manager-go",
  50. Secret: "949bbb2dd3178252638c2407578bc7ad",
  51. },
  52. Dial: xtime.Duration(time.Second),
  53. Timeout: xtime.Duration(time.Second),
  54. KeepAlive: xtime.Duration(time.Second * 10),
  55. Breaker: &breaker.Config{
  56. Window: xtime.Duration(time.Second),
  57. Sleep: xtime.Duration(time.Millisecond * 100),
  58. Bucket: 10,
  59. Ratio: 0.5,
  60. Request: 100,
  61. },
  62. },
  63. MaHTTPClient: &bm.ClientConfig{
  64. App: &bm.App{
  65. Key: "f6433799dbd88751",
  66. Secret: "36f8ddb1806207fe07013ab6a77a3935",
  67. },
  68. Dial: xtime.Duration(time.Second),
  69. Timeout: xtime.Duration(time.Second),
  70. KeepAlive: xtime.Duration(time.Second * 10),
  71. Breaker: &breaker.Config{
  72. Window: xtime.Duration(time.Second),
  73. Sleep: xtime.Duration(time.Millisecond * 100),
  74. Bucket: 10,
  75. Ratio: 0.5,
  76. Request: 100,
  77. },
  78. },
  79. Session: &SessionConfig{
  80. SessionIDLength: 32,
  81. CookieLifeTime: 1800,
  82. CookieName: "mng-go",
  83. Domain: ".bilibili.co",
  84. Memcache: &memcache.Config{
  85. Config: &pool.Config{
  86. Active: 10,
  87. Idle: 5,
  88. IdleTimeout: xtime.Duration(time.Second * 80),
  89. },
  90. Name: "go-business/auth",
  91. Proto: "tcp",
  92. Addr: "172.16.33.54:11211",
  93. DialTimeout: xtime.Duration(time.Millisecond * 1000),
  94. ReadTimeout: xtime.Duration(time.Millisecond * 1000),
  95. WriteTimeout: xtime.Duration(time.Millisecond * 1000),
  96. },
  97. },
  98. ManagerHost: "http://uat-manager.bilibili.co",
  99. DashboardHost: "http://dashboard-mng.bilibili.co",
  100. DashboardCaller: "manager-go",
  101. })
  102. }
  103. func engine() *bm.Engine {
  104. e := bm.NewServer(nil)
  105. a := getPermit()
  106. e.GET("/login", a.Verify(), func(c *bm.Context) {
  107. c.JSON("pass", nil)
  108. })
  109. e.GET("/tag/del", a.Permit("TAG_DEL"), func(c *bm.Context) {
  110. c.JSON("pass", nil)
  111. })
  112. e.GET("/tag/admin", a.Permit("TAG_ADMIN"), func(c *bm.Context) {
  113. c.JSON("pass", nil)
  114. })
  115. return e
  116. }
  117. func setSession(uid int64, username string) (string, error) {
  118. a := getPermit()
  119. sv := a.sm.newSession(context.TODO())
  120. sv.Set("username", username)
  121. mcConn := a.sm.mc.Get(context.TODO())
  122. defer mcConn.Close()
  123. key := sv.Sid
  124. item := &memcache.Item{
  125. Key: key,
  126. Object: sv,
  127. Flags: memcache.FlagJSON,
  128. Expiration: int32(a.sm.c.CookieLifeTime),
  129. }
  130. if err := mcConn.Set(item); err != nil {
  131. return "", err
  132. }
  133. return key, nil
  134. }
  135. func startEngine(t *testing.T) func() {
  136. return func() {
  137. e := engine()
  138. err := e.Run(":18080")
  139. if err != nil {
  140. t.Fatalf("failed to run server!%v", err)
  141. }
  142. }
  143. }
  144. func TestLoginSuccess(t *testing.T) {
  145. go once.Do(startEngine(t))
  146. time.Sleep(time.Millisecond * 100)
  147. sid, err := setSession(2233, "caoguoliang")
  148. if err != nil {
  149. t.Fatalf("faild to set session !err:=%v", err)
  150. }
  151. query := url.Values{}
  152. query.Set("test", "test")
  153. cli := client()
  154. req, err := cli.NewRequest("GET", "http://127.0.0.1:18080/login", "", query)
  155. if err != nil {
  156. t.Fatalf("Failed to build request: %v", err)
  157. }
  158. req.AddCookie(&http.Cookie{
  159. Name: "mng-go",
  160. Value: sid,
  161. })
  162. req.AddCookie(&http.Cookie{
  163. Name: "username",
  164. Value: "caoguoliang",
  165. })
  166. req.AddCookie(&http.Cookie{
  167. Name: "_AJSESSIONID",
  168. Value: "87fa8450e93511e79ed8522233007f8a",
  169. })
  170. res := Response{}
  171. if err := cli.Do(context.TODO(), req, &res); err != nil {
  172. t.Fatalf("Failed to send request: %v", err)
  173. }
  174. if res.Code != 0 || res.Data != "pass" {
  175. t.Fatalf("Unexpected response code(%d) data(%v)", res.Code, res.Data)
  176. }
  177. }
  178. func TestLoginFail(t *testing.T) {
  179. go once.Do(startEngine(t))
  180. time.Sleep(time.Millisecond * 100)
  181. query := url.Values{}
  182. query.Set("test", "test")
  183. cli := client()
  184. req, err := cli.NewRequest("GET", "http://127.0.0.1:18080/login", "", query)
  185. if err != nil {
  186. t.Fatalf("Failed to build request: %v", err)
  187. }
  188. req.AddCookie(&http.Cookie{
  189. Name: "mng-go",
  190. Value: "fakesess",
  191. })
  192. req.AddCookie(&http.Cookie{
  193. Name: "username",
  194. Value: "caoguoliang",
  195. })
  196. req.AddCookie(&http.Cookie{
  197. Name: "_AJSESSIONID",
  198. Value: "testsess",
  199. })
  200. res := Response{}
  201. if err := cli.Do(context.TODO(), req, &res); err != nil {
  202. t.Fatalf("Failed to send request: %v", err)
  203. }
  204. if res.Code != ecode.Unauthorized.Code() {
  205. t.Fatalf("This request should be forbidden: code(%d) data(%v)", res.Code, res.Data)
  206. }
  207. }
  208. func TestVerifySuccess(t *testing.T) {
  209. go once.Do(startEngine(t))
  210. time.Sleep(time.Millisecond * 100)
  211. sid, err := setSession(2233, "caoguoliang")
  212. if err != nil {
  213. t.Fatalf("faild to set session !err:=%v", err)
  214. }
  215. query := url.Values{}
  216. query.Set("test", "test")
  217. cli := client()
  218. req, err := cli.NewRequest("GET", "http://127.0.0.1:18080/tag/del", "", query)
  219. if err != nil {
  220. t.Fatalf("Failed to build request: %v", err)
  221. }
  222. req.AddCookie(&http.Cookie{
  223. Name: "mng-go",
  224. Value: sid,
  225. })
  226. req.AddCookie(&http.Cookie{
  227. Name: "username",
  228. Value: "caoguoliang",
  229. })
  230. req.AddCookie(&http.Cookie{
  231. Name: "_AJSESSIONID",
  232. Value: "87fa8450e93511e79ed8522233007f8a",
  233. })
  234. res := Response{}
  235. if err := cli.Do(context.TODO(), req, &res); err != nil {
  236. t.Fatalf("Failed to send request: %v", err)
  237. }
  238. if res.Code != 0 || res.Data != "pass" {
  239. t.Fatalf("Unexpected response code(%d) data(%v)", res.Code, res.Data)
  240. }
  241. }
  242. func TestVerifyFail(t *testing.T) {
  243. go once.Do(startEngine(t))
  244. time.Sleep(time.Millisecond * 100)
  245. sid, err := setSession(2233, "caoguoliang")
  246. if err != nil {
  247. t.Fatalf("faild to set session !err:=%v", err)
  248. }
  249. query := url.Values{}
  250. query.Set("test", "test")
  251. cli := client()
  252. req, err := cli.NewRequest("GET", "http://127.0.0.1:18080/tag/admin", "", query)
  253. if err != nil {
  254. t.Fatalf("Failed to build request: %v", err)
  255. }
  256. req.AddCookie(&http.Cookie{
  257. Name: "mng-go",
  258. Value: sid,
  259. })
  260. req.AddCookie(&http.Cookie{
  261. Name: "username",
  262. Value: "caoguoliang",
  263. })
  264. req.AddCookie(&http.Cookie{
  265. Name: "_AJSESSIONID",
  266. Value: "87fa8450e93511e79ed8522233007f8a",
  267. })
  268. res := Response{}
  269. if err := cli.Do(context.TODO(), req, &res); err != nil {
  270. t.Fatalf("Failed to send request: %v", err)
  271. }
  272. if res.Code != ecode.AccessDenied.Code() {
  273. t.Fatalf("This request should be forbidden: code(%d) data(%v)", res.Code, res.Data)
  274. }
  275. }