Beispiel #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)
}
// TestCreateDb tests the creation and initial seeding of the database.
func TestCreateDb(t *testing.T) {
	db, err := database.InitializeTestDB("db-seed.toml")
	if err != nil {
		t.Fatal("Failed to initialize database:", err)
	}
	defer db.Close()

	errPing := db.Ping()
	if errPing != nil {
		t.Fatal("Failed to ping database:", errPing)
	}

	var sites database.Sites
	// Get all of the active sites
	err = sites.GetSites(db, true, false)
	if err != nil {
		t.Error("Failed to get all the sites.", err)
	}

	// Verify that there are  two active sites loaded.
	if len(sites) != 2 {
		t.Error("There should be two active sites loaded.")
	}

	// Verify that GetFirstPing doesn't throw an error with empty Pings
	firstPing, err := sites[0].GetFirstPing(db)
	if err != nil {
		t.Error("GetFirstPing shouldn't throw error if empty pings: ", err)
	}

	zeroTime := time.Time{}
	if firstPing != zeroTime {
		t.Error("GetFirstPing should return a zero time for an empty ping table, but returned: ", err)
	}
}
func getAllSites(controller *contactsController) (database.Sites, error) {
	// Get all of the sites to display in the sites-to-assign table.
	var sites database.Sites
	err := sites.GetSites(controller.DB, false, false)
	if err != nil {
		return nil, err
	}
	return sites, nil
}
// GetSites provides the implementation of the SitesGetter type for runtime usage.
func GetSites(db *sql.DB) (database.Sites, error) {
	var sites database.Sites
	// Get active sites with contacts.
	err := sites.GetSites(db, true, true)
	if err != nil {
		return nil, err
	}
	return sites, nil
}
func (controller *settingsController) get(rw http.ResponseWriter, req *http.Request) (int, error) {
	var sites database.Sites
	// Get all of the sites, including inactive ones, and the contacts.
	err := sites.GetSites(controller.DB, false, true)
	if err != nil {
		return http.StatusInternalServerError, err
	}
	isAuthenticated, user := getCurrentUser(rw, req, controller.authorizer)
	vm := viewmodels.GetSettingsViewModel(sites, isAuthenticated, user, err)
	return http.StatusOK, controller.template.Execute(rw, vm)
}
// TestCreateAndGetMultipleSites tests creating more than one active sites
// with contacts in the database and then retrieving them.
func TestCreateAndGetMultipleSites(t *testing.T) {
	var err error
	db, err := database.InitializeTestDB("")
	if err != nil {
		t.Fatal("Failed to create database:", err)
	}
	defer db.Close()

	// Create the first site.
	s1 := database.Site{Name: "Test", IsActive: true, URL: "http://www.google.com",
		PingIntervalSeconds: 60, TimeoutSeconds: 30, ContentExpected: "Expected 1",
		ContentUnexpected: "Unexpected 1"}
	err = s1.CreateSite(db)
	if err != nil {
		t.Fatal("Failed to create first site:", err)
	}

	// Create the second site.
	s2 := database.Site{Name: "Test 2", IsActive: true, URL: "http://www.test.com",
		PingIntervalSeconds: 60, TimeoutSeconds: 30,
		LastStatusChange: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
		LastPing:         time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
		ContentExpected:  "Expected 2", ContentUnexpected: "Unexpected 2"}
	err = s2.CreateSite(db)
	if err != nil {
		t.Fatal("Failed to create second site:", err)
	}

	// Create a third site that is marked inactive.
	s3 := database.Site{Name: "Test 3", IsActive: false, URL: "http://www.test3.com",
		PingIntervalSeconds: 60, TimeoutSeconds: 30, ContentExpected: "Expected 3",
		ContentUnexpected: "Unexpected 3"}
	err = s3.CreateSite(db)
	if err != nil {
		t.Fatal("Failed to create third site:", err)
	}

	// Create first contact
	c1 := database.Contact{Name: "Joe Contact", EmailAddress: "*****@*****.**", SmsNumber: "5125551212",
		SmsActive: false, EmailActive: false}
	err = c1.CreateContact(db)
	if err != nil {
		t.Fatal("Failed to create new contact:", err)
	}
	// Associate to the first and second site ID
	err = s1.AddContactToSite(db, c1.ContactID)
	if err != nil {
		t.Fatal("Failed to associate contact 1 with first site:", err)
	}
	err = s2.AddContactToSite(db, c1.ContactID)
	if err != nil {
		t.Fatal("Failed to associate contact 1 with second site:", err)
	}

	// Create second contact
	c2 := database.Contact{Name: "Jack Contact", EmailAddress: "*****@*****.**", SmsNumber: "5125551213",
		SmsActive: false, EmailActive: false}
	err = c2.CreateContact(db)
	if err != nil {
		t.Fatal("Failed to create new contact:", err)
	}
	// Associate only to the first site
	err = s1.AddContactToSite(db, c2.ContactID)
	if err != nil {
		t.Fatal("Failed to associate contact 1 with first site:", err)
	}

	var sites database.Sites
	// Get active sites with contacts
	err = sites.GetSites(db, true, true)
	if err != nil {
		t.Fatal("Failed to get all the sites.", err)
	}

	// Verify that there are only two active sites.
	if len(sites) != 2 {
		t.Fatal("There should only be two active sites loaded.")
	}

	// Verify the first site was Loaded with proper attributes.
	if !database.CompareSites(s1, sites[0]) {
		t.Fatal("First saved site not equal to input:\n", sites[0], s1)
	}

	// Verify the second site was Loaded with proper attributes.
	if !database.CompareSites(s2, sites[1]) {
		t.Fatal("Second saved site not equal to input:\n", sites[1], s2)
	}

	// Verify the first contact was Loaded with proper attributes and sorted last.
	if !reflect.DeepEqual(c1, sites[0].Contacts[1]) {
		t.Error("Second saved contact not equal to input:\n", sites[0].Contacts[1], c1)
	}
	// Verify the second contact was loaded with the proper attributes and sorted first.
	if !reflect.DeepEqual(c2, sites[0].Contacts[0]) {
		t.Error("First saved contact not equal to input:\n", sites[0].Contacts[0], c2)
	}
	// Verify the first contact was loaded to the second site.
	if !reflect.DeepEqual(c1, sites[1].Contacts[0]) {
		t.Error("Second saved contact not equal to input:\n", sites[1].Contacts[0], c1)
	}

	// Verify that the first contact can get both related sites
	err = c1.GetContactSites(db)
	if err != nil {
		t.Error("Error getting the sites for the first contact.")
	}
	if s1.URL != c1.Sites[0].URL {
		t.Error("First contact's first site not as expected:\n", c1.Sites[0].URL, s1.URL)
	}
	if s2.URL != c1.Sites[1].URL {
		t.Error("First contact's second site not as expected:\n", c1.Sites[1].URL, s2.URL)
	}
	if len(c1.Sites) != 2 {
		t.Error("First contact should have two associated site.")
	}

	// Verify that the second contact can get the only related sites
	err = c2.GetContactSites(db)
	if err != nil {
		t.Error("Error getting the site for the second contact.")
	}
	if s1.URL != c2.Sites[0].URL {
		t.Error("Second contact's first site not as expected:\n", c2.Sites[0].URL, s1.URL)
	}
	if len(c2.Sites) != 1 {
		t.Error("Second contact should only have one associated site.")
	}

	// Test for just the active sites without the contacts
	var sitesNoContacts database.Sites
	err = sitesNoContacts.GetSites(db, true, false)
	if err != nil {
		t.Fatal("Failed to get all the sites.", err)
	}

	// Verify the first site was Loaded with proper attributes and no contacts.
	if !database.CompareSites(s1, sitesNoContacts[0]) {
		t.Error("First saved site not equal to GetActiveSites results:\n", sitesNoContacts[0], s1)
	}

	// Verify the second site was Loaded with proper attributes and no contacts.
	if !database.CompareSites(s2, sitesNoContacts[1]) {
		t.Error("Second saved site not equal to GetActiveSites results:\n", sitesNoContacts[1], s2)
	}

	// Test for all of the sites without the contacts
	var allSitesNoContacts database.Sites
	err = allSitesNoContacts.GetSites(db, false, false)
	if err != nil {
		t.Fatal("Failed to get all of the sites.", err)
	}

	// Verify that there are 3 total sites.
	if len(allSitesNoContacts) != 3 {
		t.Error("There should be three total sites loaded.")
	}

}