create_resources_test.go 6.68 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
package integrationtest

import (
	"testing"

	as "git.autistici.org/ai3/accountserver"
)

func TestIntegration_CreateResources(t *testing.T) {
	stop, _, c := startService(t)
	defer stop()

	testdata := []struct {
		resource   *as.Resource
		expectedOk bool
	}{
		// Create a domain resource.
		{
			&as.Resource{
				Name:          "example2.com",
				Type:          as.ResourceTypeDomain,
				Status:        as.ResourceStatusActive,
				Shard:         "host2",
				OriginalShard: "host2",
				Website: &as.Website{
					URL:          "https://example2.com",
					DocumentRoot: "/home/users/investici.org/uno/html-example2.com",
					AcceptMail:   true,
				},
			},
			true,
		},

		// Duplicate of the above request, should fail due to conflict.
		{
			&as.Resource{
				Type:          as.ResourceTypeDomain,
				Name:          "example2.com",
				Status:        as.ResourceStatusActive,
				Shard:         "host2",
				OriginalShard: "host2",
				Website: &as.Website{
					URL:          "https://example2.com",
					DocumentRoot: "/home/users/investici.org/uno/html-example2.com",
				},
			},
			false,
		},

		// Empty document root will be fixed by templating.
		{
			&as.Resource{
				Type:          as.ResourceTypeDomain,
				Name:          "example3.com",
				Status:        as.ResourceStatusActive,
				Shard:         "host2",
				OriginalShard: "host2",
				Website:       &as.Website{},
			},
			true,
		},

		// Malformed resource metadata (name fails validation).
		{
			&as.Resource{
				Type:          as.ResourceTypeDomain,
				Name:          "example$.com",
				Status:        as.ResourceStatusActive,
				Shard:         "host2",
				OriginalShard: "host2",
				Website: &as.Website{
					URL:          "https://example$.com",
					DocumentRoot: "/home/users/investici.org/uno/html-example3.com",
				},
			},
			false,
		},

		// Bad shard.
		{
			&as.Resource{
				Type:          as.ResourceTypeDomain,
				Name:          "example4.com",
				Status:        as.ResourceStatusActive,
				Shard:         "zebra",
				OriginalShard: "zebra",
				Website: &as.Website{
					URL:          "https://example4.com",
					DocumentRoot: "/home/users/investici.org/uno/html-example4.com",
				},
			},
			false,
		},

		// The document root has no associated DAV account.
		{
			&as.Resource{
				Type:          as.ResourceTypeDomain,
				Name:          "example5.com",
				Status:        as.ResourceStatusActive,
				Shard:         "host2",
				OriginalShard: "host2",
				Website: &as.Website{
					URL:          "https://example5.com",
					DocumentRoot: "/home/users/investici.org/nonexisting",
				},
			},
			false,
		},
	}

	for _, td := range testdata {
		err := c.request("/api/resource/create", &as.CreateResourcesRequest{
114 115 116
			AdminRequestBase: as.AdminRequestBase{
				RequestBase: as.RequestBase{
					SSO: c.ssoTicket(testAdminUser),
117 118
				},
			},
119
			Username:  "uno@investici.org",
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
			Resources: []*as.Resource{td.resource},
		}, nil)
		if err == nil && !td.expectedOk {
			t.Errorf("CreateResource(%s) should have failed but didn't", td.resource.ID)
		} else if err != nil && td.expectedOk {
			t.Errorf("CreateResource(%s) failed: %v", td.resource.ID, err)
		}
	}
}

func TestIntegration_CreateMultipleResources(t *testing.T) {
	stop, _, c := startService(t)
	defer stop()

	testdata := []struct {
		name       string
		username   string
		resources  []*as.Resource
		expectedOk bool
	}{
		{
			// The create request is very bare, most values will be
			// filled in by the server using resource
			// templates. Note that we specify client-side IDs and
			// use them in ParentIDs to specify nesting.
			"site_with_db",
			"uno@investici.org",
			[]*as.Resource{
				&as.Resource{
					ID:   as.ResourceID("example3_site"),
					Type: as.ResourceTypeDomain,
					Name: "example3.com",
				},
				&as.Resource{
					Type: as.ResourceTypeDAV,
					Name: "example3dav",
				},
				&as.Resource{
					Type:     as.ResourceTypeDatabase,
					ParentID: as.ResourceID("example3_site"),
					Name:     "example3",
				},
			},
			true,
		},

166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
		{
			// Same as above, but without an associated user.
			"site_with_db_no_user",
			"",
			[]*as.Resource{
				&as.Resource{
					ID:   as.ResourceID("example3_site"),
					Type: as.ResourceTypeDomain,
					Name: "example3.com",
				},
				&as.Resource{
					Type: as.ResourceTypeDAV,
					Name: "example3dav",
				},
				&as.Resource{
					Type:     as.ResourceTypeDatabase,
					ParentID: as.ResourceID("example3_site"),
					Name:     "example3",
				},
			},
			false,
		},

189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
		{
			// An attempt to create resources without client-side
			// IDs. It should fail the database validation.
			"site_with_db_no_nesting",
			"uno@investici.org",
			[]*as.Resource{
				&as.Resource{
					Type: as.ResourceTypeDomain,
					Name: "example4.com",
				},
				&as.Resource{
					Type: as.ResourceTypeDAV,
					Name: "example4dav",
				},
				&as.Resource{
					Type: as.ResourceTypeDatabase,
					Name: "example4",
				},
			},
			false,
		},

		{
			// An attempt to create a website without an associated
			// DAV account, it should pick up the DAV account that
			// already exists for user #1.
			"site_with_db_no_dav_picks_up_existing_account",
			"uno@investici.org",
			[]*as.Resource{
				&as.Resource{
					ID:   as.ResourceID("example5_site"),
					Type: as.ResourceTypeDomain,
					Name: "example5.com",
				},
				&as.Resource{
					ParentID: as.ResourceID("example5_site"),
					Type:     as.ResourceTypeDatabase,
					Name:     "example5",
				},
			},
			true,
		},

		{
			// An attempt to create a website without an associated
			// DAV account, for a user without an existing DAV
			// account. Should fail.
			"site_with_db_no_dav_and_no_existing_account",
			"tre@investici.org",
			[]*as.Resource{
				&as.Resource{
					ID:   as.ResourceID("example6_site"),
					Type: as.ResourceTypeDomain,
					Name: "example6.com",
				},
				&as.Resource{
					ParentID: as.ResourceID("example6_site"),
					Type:     as.ResourceTypeDatabase,
					Name:     "example6",
				},
			},
			false,
		},
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267

		{
			// Resource without an owner.
			"list",
			"",
			[]*as.Resource{
				&as.Resource{
					Type: as.ResourceTypeMailingList,
					Name: "list1@example.com",
					List: &as.MailingList{
						Admins: []string{"uno@investici.org"},
					},
				},
			},
			true,
		},
268 269 270 271
	}

	for _, td := range testdata {
		req := &as.CreateResourcesRequest{
272 273 274
			AdminRequestBase: as.AdminRequestBase{
				RequestBase: as.RequestBase{
					SSO: c.ssoTicket(testAdminUser),
275 276
				},
			},
277
			Username:  td.username,
278 279 280 281 282 283 284 285 286 287
			Resources: td.resources,
		}
		err := c.request("/api/resource/create", req, nil)
		if err == nil && !td.expectedOk {
			t.Errorf("'%s' should have failed but didn't", td.name)
		} else if err != nil && td.expectedOk {
			t.Errorf("'%s' failed: %v", td.name, err)
		}
	}
}