예제 #1
0
func (controller *homeController) get(rw http.ResponseWriter, req *http.Request) (int, error) {
	var sites database.Sites
	// Get active sites with no contacts.
	err := sites.GetSites(controller.DB, true, false)
	if err != nil {
		return http.StatusInternalServerError, err
	}

	// Check if all of the active sites have a FirstPing that is not zero.  If so
	// then get the first ping from the database, if available.
	for i, site := range sites {
		if site.FirstPing.IsZero() {
			firstPing, err := site.GetFirstPing(controller.DB)
			if err != nil {
				return http.StatusInternalServerError, err
			}
			err = site.UpdateSiteFirstPing(controller.DB, firstPing)
			if err != nil {
				return http.StatusInternalServerError, err
			}
			sites[i].FirstPing = firstPing
		}
	}
	isAuthenticated, user := getCurrentUser(rw, req, controller.authorizer)
	messages := controller.authorizer.Messages(rw, req)
	vm := viewmodels.GetHomeViewModel(sites, isAuthenticated, user, messages)
	return http.StatusOK, controller.template.Execute(rw, vm)
}
예제 #2
0
// TestGetHomeViewModel tests the view model is created as expected.
func TestGetHomeViewModel(t *testing.T) {
	sites := database.Sites{}
	user := httpauth.UserData{}

	// First site has no last status change.
	sites = append(sites, database.Site{Name: "Test 1", IsSiteUp: true})
	// Second site was down 2 hours ago.
	now := time.Now()
	twoHoursAgo := now.Add(-2 * time.Hour)
	sites = append(sites, database.Site{Name: "Test 2", IsSiteUp: false, LastStatusChange: twoHoursAgo})
	// Third site was up 2 days ago.
	twodaysAgo := now.Add(-48 * time.Hour)
	sites = append(sites, database.Site{Name: "Test 3", IsSiteUp: true, LastStatusChange: twodaysAgo})
	// Fourth site has no last status change but does have first ping three days ago.
	threedaysAgo := now.Add(-72 * time.Hour)
	sites = append(sites, database.Site{Name: "Test 4", IsSiteUp: true, FirstPing: threedaysAgo})

	result := viewmodels.GetHomeViewModel(sites, false, user, nil)
	if result.Nav.Active != "home" {
		t.Error("Home View Model Active returned incorrect value")
	}

	if result.Title != "Go Ping Sites - Home" {
		t.Error("Home View Model Sites returned incorrect value")
	}

	if result.Sites[0].CSSClass != "success" || result.Sites[0].Status != "Up" ||
		result.Sites[0].HowLong != "Unknown" || result.Sites[0].Name != "Test 1" {
		t.Error("First site returned incorrect values")
	}

	if result.Sites[1].CSSClass != "danger" || result.Sites[1].Status != "Down" ||
		result.Sites[1].HowLong != "2 hours ago" || result.Sites[1].Name != "Test 2" {
		t.Error("Second site returned incorrect values")
	}

	if result.Sites[2].CSSClass != "success" || result.Sites[2].Status != "Up" ||
		result.Sites[2].HowLong != "2 days ago" || result.Sites[2].Name != "Test 3" ||
		result.Sites[2].HasNoStatusChanges {
		t.Error("Third site returned incorrect values")
	}

	if result.Sites[3].CSSClass != "success" || result.Sites[3].Status != "Up" ||
		result.Sites[3].HowLong != "3 days ago" || result.Sites[3].Name != "Test 4" ||
		!result.Sites[3].HasNoStatusChanges {
		t.Error("Fourth site returned incorrect values")
	}

	if result.HasSiteWithNoStatusChanges != true {
		t.Error("Should indicate has site with no status change.")
	}

}
예제 #3
0
// TestGetHomeViewModel tests the view model is created as expected.
func TestGetHomeViewModelWithStatusChanges(t *testing.T) {
	sites := database.Sites{}
	user := httpauth.UserData{}

	// First site was up 2 hours ago.
	now := time.Now()
	twoHoursAgo := now.Add(-2 * time.Hour)
	sites = append(sites, database.Site{Name: "Test 1", IsSiteUp: true, LastStatusChange: twoHoursAgo})
	// Second site was down 2 hours ago.
	sites = append(sites, database.Site{Name: "Test 2", IsSiteUp: false, LastStatusChange: twoHoursAgo})
	// Third site was up 2 days ago.
	twodaysAgo := now.Add(-48 * time.Hour)
	sites = append(sites, database.Site{Name: "Test 3", IsSiteUp: true, LastStatusChange: twodaysAgo})

	result := viewmodels.GetHomeViewModel(sites, false, user, nil)

	if result.HasSiteWithNoStatusChanges == true {
		t.Error("Should NOT indicate has site with no status change.")
	}
}