Exemplo n.º 1
0
func TestResetRequestHandler(t *testing.T) {
	mock, err := db.MockDB()
	if err != nil {
		t.Fatal("Could not Mock DB:", err)
	}

	var email string
	if email = mock.Email; email == "" {
		t.Fatal("No email")
	}

	req, err := http.NewRequest("GET", "http://broome.io/reset/"+email, nil)
	if err != nil {
		t.Fatal("Could not create request:", err)
	}

	res := httptest.NewRecorder()
	broomeServer(res, req)

	if res.Code != http.StatusOK {
		t.Fatalf("Non-expected status code: %v\tbody: %v", res.Code, res.Body)
	}

	resBody := map[string]interface{}{}
	if err := json.Unmarshal([]byte(res.Body.String()), &resBody); err != nil {
		t.Fatal("Response is not valid JSON", err)
	}

	if resBody["status"] != "success" {
		t.Fatal("response status should be 'created' not ", resBody["status"])
	}
}
Exemplo n.º 2
0
func TestUpdateDeveloperHandler(t *testing.T) {
	mock, err := db.MockDB()
	if err != nil {
		t.Fatal("Could not Mock DB:", err)
	}

	var token string
	if token = mock.Token; token == "" {
		t.Fatal("No token")
	}

	req, err := http.NewRequest("PUT", "http://broome.io/developers/"+token, nil)
	if err != nil {
		t.Fatal("Could not Create Request", err)
	}
	req.SetBasicAuth(token, "")
	req.PostForm = url.Values{
		"name":        {"David"},
		"oldpassword": {"java$cript"},
		"password":    {"newpass"},
	}
	res := httptest.NewRecorder()
	broomeServer(res, req)

	if res.Code != http.StatusOK {
		t.Fatalf("Non-expected status code: %v\tbody: %v", res.Code, res.Body)
	}

	body := map[string]interface{}{}
	if err := json.Unmarshal([]byte(res.Body.String()), &body); err != nil {
		t.Fatal("Response is not valid JSON", err)
	}

	if body["status"] != "updated" {
		t.Fatal("response status should be 'updated' not ", body["status"])
	}

	if reflect.DeepEqual(body["update"], req.PostForm) {
		t.Fatal("response update is not the same as request update", body["update"], req.PostForm)
	}
}
Exemplo n.º 3
0
func TestPasswordEditHandler(t *testing.T) {
	mock, err := db.MockDB()
	if err != nil {
		t.Fatal("Could not Mock DB:", err)
	}

	var id bson.ObjectId
	fmt.Printf("%T", mock.ID)
	id = mock.ID

	var token string
	if token = mock.Token; token == "" {
		t.Fatal("Invalid token")
	}

	req, err := http.NewRequest("PUT", "http://broome.io/developers/reset/"+token, nil)
	if err != nil {
		t.Fatal("Could not Create Request", err)
	}
	req.PostForm = url.Values{
		"id":  {id.Hex()},
		"new": {"password"},
	}
	res := httptest.NewRecorder()
	broomeServer(res, req)

	if res.Code != http.StatusOK {
		t.Fatalf("Non-expected status code: %v\tbody: %v", res.Code, res.Body)
	}

	body := map[string]interface{}{}
	if err := json.Unmarshal([]byte(res.Body.String()), &body); err != nil {
		t.Fatal("Response is not valid JSON", err)
	}

	if body["status"] != "success" {
		t.Fatal("response status should be 'updated' not ", body["status"])
	}
}
Exemplo n.º 4
0
func TestDeveloperMeHandler(t *testing.T) {
	mock, err := db.MockDB()
	if err != nil {
		t.Fatal("Could not Mock DB:", err)
	}

	var token string
	if token = mock.Token; token == "" {
		t.Fatal("No token.")
	}

	req, err := http.NewRequest("GET", "http://broome.io/developers/me?token="+token, nil)
	if err != nil {
		t.Fatal("Could not Create Request", err)
	}

	res := httptest.NewRecorder()
	broomeServer(res, req)

	if res.Code != http.StatusOK {
		t.Fatalf("Non-expected status code: %v\tbody: %v", res.Code, res.Body)
	}

	body := &requests.DeveloperRes{}
	if err := json.Unmarshal([]byte(res.Body.String()), body); err != nil {
		t.Fatal("Response is not valid JSON", err)
	}

	if body.Status != "found" {
		t.Fatalf("response status should be 'created' not %v. Error: %v ", body.Status, body.Error)
	}

	var expCreatedAt int64 = 1390922819901

	if body.Developer.CreatedAt != expCreatedAt {
		t.Fatalf("Developer has changed: Date expected %f but got %d", expCreatedAt, body.Developer.CreatedAt)
	}
}
Exemplo n.º 5
0
func TestCreateTokenHandler(t *testing.T) {
	_, err := db.MockDB()
	if err != nil {
		t.Fatal("Could not Mock DB:", err)
	}

	var body bytes.Buffer
	bodyReq := map[string]interface{}{"Email": "*****@*****.**", "Password": "******"}

	encoder := json.NewEncoder(&body)
	err = encoder.Encode(bodyReq)
	if err != nil {
		t.Fatal("Could not encode JSON:", err)
	}

	req, err := http.NewRequest("POST", "http://broome.io/developers/token", &body)
	if err != nil {
		t.Fatal("Could not create request:", err)
	}
	defer req.Body.Close()

	res := httptest.NewRecorder()
	broomeServer(res, req)

	if res.Code != http.StatusOK {
		t.Fatalf("Non-expected status code: %v\tbody: %v", res.Code, res.Body)
	}

	resBody := map[string]interface{}{}
	if err := json.Unmarshal([]byte(res.Body.String()), &resBody); err != nil {
		t.Fatal("Response is not valid JSON", err)
	}

	if resBody["status"] != "created" {
		t.Fatal("response status should be 'created' not ", resBody["status"])
	}
}