func init() { var err error pmTestUser, err = model.NewUser("test pm", "*****@*****.**", "https://patrolavia.com/logo128.png") if err != nil { log.Fatalf("Cannot create user for testing pad modification: %s", err) } pmCoop, err = model.NewUser("test pm coop", "*****@*****.**", "https://patrolavia.com/logo128.png") if err != nil { log.Fatalf("Cannot create cooperator for testing pad modification: %s", err) } }
func init() { var err error pvTestUser, err = model.NewUser("test pv", "*****@*****.**", "https://patrolavia.com/logo128.png") if err != nil { log.Fatalf("Cannot create user to test pad version control: %s", err) } }
func init() { var err error padTestUser, err = model.NewUser("test pad", "*****@*****.**", "https://patrolavia.com/logo128.png") if err != nil { log.Fatalf("Cannot create user for testing pad: %s", err) } }
func init() { var err error peAclUser, err = model.NewUser("test pe user", "*****@*****.**", "https://patrolavia.com/logo128.png") if err != nil { log.Fatalf("Cannot create user for testing pad edit acl: %s", err) } peAclCoop, err = model.NewUser("test pe coop", "*****@*****.**", "https://patrolavia.com/logo128.png") if err != nil { log.Fatalf("Cannot create coop for testing pad edit acl: %s", err) } peAclGuest, err = model.NewUser("test pe guest", "*****@*****.**", "https://patrolavia.com/logo128.png") if err != nil { log.Fatalf("Cannot create guest for testing pad edit acl: %s", err) } peAclPad, err = model.NewPad(db, peAclUser.ID, "pe acl", "test pe acl", nil, []int{peAclCoop.ID}) if err != nil { log.Fatalf("Cannot create pad for testing pad edit act: %s", err) } peAclURI = fmt.Sprintf("/api/edit/%d", peAclPad.ID) }
func TestValidEditor(t *testing.T) { cfg := map[string]string{ "RedisAddr": ":6379", "SessSecret": "1234567890", "SessName": "mdpadtest", "ValidEditors": "*****@*****.**", } pc := &Pad{db, sf, cfg} u, err := model.NewUser("invalid editor", "*****@*****.**", "https://patrolavia.com/logo128.png") if err != nil { t.Fatalf("Cannot create invalid user to test valid editor: %s", err) } vu, err := model.NewUser("valid editor", "*****@*****.**", "https://patrolavia.com/logo128.png") if err != nil { t.Fatalf("Cannot create valid user to test valid editor: %s", err) } sess := session(sf, t) sess.Login(u) sess.Save() if err := sess.Err(); err != nil { t.Fatalf("Cannot login as invalid editor: %s", err) } pad := map[string]interface{}{ "title": "invalid edit", "content": "test invalid edit", } resp, err := jsonapi.HandlerTest(pc.Create).PostJSON("/api/create", sess.Cookie(), pad) if err != nil { t.Fatalf("Failed to get response of creating pad with invalid editor: %s", err) } if !testResult(resp.Body, false) { t.Errorf("api call should fail when invalid editor tries to create pad: %s", resp.Body.String()) } if !testData(3, resp.Body, "code") { t.Errorf("api call should return errcode 1 when invalid editor tries to create pad: %s", resp.Body.String()) } sess.Login(vu) sess.Save() if err := sess.Err(); err != nil { t.Fatalf("Cannot login as valid editor: %s", err) } resp, err = jsonapi.HandlerTest(pc.Create).PostJSON("/api/create", sess.Cookie(), pad) if err != nil { t.Fatalf("Failed to get response of creating pad with invalid editor: %s", err) } if !testResult(resp.Body, true) { t.Errorf("api call should be ok when valid editor tries to create pad: %s", resp.Body.String()) } if !testData(0, resp.Body, "code") { t.Errorf("api call should return errcode 0 when valid editor tries to create pad: %s", resp.Body.String()) } }
// GoogleCallback is callback for google oauth func (ac *Auth) GoogleCallback(w http.ResponseWriter, r *http.Request) { sess := ac.SF.Get(r) stat := sess.Get("login_token") sess.Unset("login_token") if err := sess.Err(); err != nil { common.Fatalf(w, err, "Cannot read token from session") return } conf := ac.googleConfig(r) _ = r.ParseForm() code := r.Form.Get("code") state := r.Form.Get("state") if state != stat { http.Error(w, "Token mismatch!", http.StatusBadRequest) return } tok, err := conf.Exchange(oauth2.NoContext, code) if err != nil { common.Errorf(err, "Error occurs when exchanging token") http.Redirect(w, r, ac.Config.URL("/"), http.StatusTemporaryRedirect) return } client := conf.Client(oauth2.NoContext, tok) p, _ := plus.New(client) me, err := p.People.Get("me").Do() if err != nil { common.Errorf(err, "Error occurs when getting user info") http.Redirect(w, r, ac.Config.URL("/"), http.StatusTemporaryRedirect) return } var email string for _, e := range me.Emails { if email == "" { email = e.Value } if e.Type == "account" { email = e.Value break } } u, err := model.FindUser(email) if err != nil { common.Errorf(err, "Faile to save user, trying create one for %s.", email) u, err = model.NewUser(me.DisplayName, email, me.Image.Url) if err != nil { common.Fatalf(w, err, "Failed to login, please try again later.") return } } sess.Set("uid", fmt.Sprint(u.ID)) sess.Save(r, w) if err := sess.Err(); err != nil { common.Fatal(w, err, "Cannot save user id in session") return } http.Redirect(w, r, ac.Config.URL("/"), http.StatusTemporaryRedirect) }