Exemplo n.º 1
0
func getContactSiteIDs(controller *contactsController, contact *database.Contact) ([]int64, error) {
	// Get the site ID's for a given contact
	siteIDs := []int64{}
	err := contact.GetContactSites(controller.DB)
	if err != nil {
		return nil, err
	}
	for _, site := range contact.Sites {
		siteIDs = append(siteIDs, site.SiteID)
	}
	return siteIDs, nil
}
// 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.")
	}

}