Ejemplo n.º 1
0
func TestCategories(t *testing.T) {
	db := mustDB(t)
	a := &Category{Name: "a"}
	if err := db.SaveCategory(a); err != nil {
		t.Fatal(err)
	}
	b := &Category{Name: "b", ParentID: a.ID}
	if err := db.SaveCategory(b); err != nil {
		t.Fatal(err)
	}
	want := CategoryMap{a.ID: a, b.ID: b}
	if got, err := db.Categories(); err != nil {
		t.Error(err)
	} else if diff := pretty.Compare(got, want); diff != "" {
		t.Fatal(diff)
	}
	a.Name, b.Name = "c", "d"
	if err := db.SaveCategory(a); err != nil {
		t.Fatal(err)
	} else if err := db.SaveCategory(b); err != nil {
		t.Fatal(err)
	} else if got, err := db.Categories(); err != nil {
		t.Error(err)
	} else if diff := pretty.Compare(got, want); diff != "" {
		t.Fatal(diff)
	}
}
Ejemplo n.º 2
0
// TestInstrs tests that each instruction behaves as expected through one
// instruction cycle.
func TestInstrs(t *testing.T) {
	for _, tc := range instructions {
		var m []*metrics.Metric
		m = append(m,
			metrics.NewMetric("foo", "test", metrics.Counter),
			metrics.NewMetric("bar", "test", metrics.Counter))

		v := New(tc.name, tc.re, tc.str, m, []instr{tc.i}, true)
		v.t = new(thread)
		v.t.stack = make([]interface{}, 0)
		for _, item := range tc.reversedStack {
			v.t.Push(item)
		}
		v.t.matches = make(map[int][]string, 0)
		v.input = "aaaab"
		v.execute(v.t, tc.i)

		diff := pretty.Compare(tc.expectedStack, v.t.stack)
		if len(diff) > 0 {
			t.Errorf("%s: unexpected virtual machine stack state.\n%s", tc.name, diff)
		}
		// patch in the thread stack because otherwise the test table is huge
		tc.expectedThread.stack = tc.expectedStack

		if diff = pretty.Compare(v.t, &tc.expectedThread); len(diff) > 0 {
			t.Errorf("%s: unexpected virtual machine thread state.\n%s", tc.name, diff)
		}
	}
}
Ejemplo n.º 3
0
func runOAuth2IdentityTests(t *testing.T, conn oauth2Connector, tests []oauth2IdentityTest) {
	for i, tt := range tests {
		f := func(req *http.Request) (*http.Response, error) {
			resp, ok := tt.urlResps[req.URL.String()]
			if !ok {
				return nil, fmt.Errorf("unexpected request URL: %s", req.URL.String())
			}
			return &http.Response{
				StatusCode: resp.statusCode,
				Body:       ioutil.NopCloser(strings.NewReader(resp.body)),
			}, nil
		}
		got, err := conn.Identity(fakeClient(f))
		if tt.wantErr == nil {
			if err != nil {
				t.Errorf("case %d: failed to get identity=%v", i, err)
				continue
			}
			if diff := pretty.Compare(tt.want, got); diff != "" {
				t.Errorf("case %d: Compare(want, got) = %v", i, diff)
			}
		} else {
			if err == nil {
				t.Errorf("case %d: want error=%v, got=<nil>", i, tt.wantErr)
				continue
			}
			if diff := pretty.Compare(tt.wantErr, err); diff != "" {
				t.Errorf("case %d: Compare(wantErr, gotErr) = %v", i, diff)
			}
		}
	}
}
Ejemplo n.º 4
0
func TestMetricToGraphite(t *testing.T) {
	ts, terr := time.Parse("2006/01/02 15:04:05", "2012/07/24 10:14:00")
	if terr != nil {
		t.Errorf("time parse error: %s", terr)
	}

	scalarMetric := metrics.NewMetric("foo", "prog", metrics.Counter)
	d, _ := scalarMetric.GetDatum()
	d.Set(37, ts)
	r := FakeSocketWrite(metricToGraphite, scalarMetric)
	expected := []string{"prog.foo 37 1343124840\n"}
	diff := pretty.Compare(r, expected)
	if len(diff) > 0 {
		t.Errorf("String didn't match:\n%s", diff)
	}

	dimensionedMetric := metrics.NewMetric("bar", "prog", metrics.Gauge, "l")
	d, _ = dimensionedMetric.GetDatum("quux")
	d.Set(37, ts)
	d, _ = dimensionedMetric.GetDatum("snuh")
	d.Set(37, ts)
	r = FakeSocketWrite(metricToGraphite, dimensionedMetric)
	expected = []string{
		"prog.bar.l.quux 37 1343124840\n",
		"prog.bar.l.snuh 37 1343124840\n"}
	diff = pretty.Compare(r, expected)
	if len(diff) > 0 {
		t.Errorf("String didn't match:\n%s", diff)
	}
}
Ejemplo n.º 5
0
func TestNewPasswordReset(t *testing.T) {
	clock = clockwork.NewFakeClock()
	defer func() {
		clock = clockwork.NewRealClock()
	}()

	now := clock.Now()

	issuer, _ := url.Parse("http://example.com")
	clientID := "myclient"
	usr := User{ID: "123456", Email: "*****@*****.**"}
	callback := "http://client.example.com/callback"
	expires := time.Hour * 3
	password := Password("passy")

	tests := []struct {
		user     User
		password Password
		issuer   url.URL
		clientID string
		callback string
		expires  time.Duration
		want     jose.Claims
	}{
		{
			issuer:   *issuer,
			clientID: clientID,
			user:     usr,
			callback: callback,
			expires:  expires,
			password: password,
			want: map[string]interface{}{
				"iss": issuer.String(),
				"aud": clientID,
				ClaimPasswordResetCallback: callback,
				ClaimPasswordResetPassword: string(password),
				"exp": float64(now.Add(expires).Unix()),
				"sub": usr.ID,
				"iat": float64(now.Unix()),
			},
		},
	}

	for i, tt := range tests {
		cbURL, err := url.Parse(tt.callback)
		if err != nil {
			t.Fatalf("case %d: non-nil err: %q", i, err)
		}
		ev := NewPasswordReset(tt.user, tt.password, tt.issuer, tt.clientID, *cbURL, tt.expires)

		if diff := pretty.Compare(tt.want, ev.claims); diff != "" {
			t.Errorf("case %d: Compare(want, got): %v", i, diff)
		}

		if diff := pretty.Compare(ev.Password(), password); diff != "" {
			t.Errorf("case %d: Compare(want, got): %v", i, diff)
		}
	}
}
Ejemplo n.º 6
0
func TestRouteLookup(t *testing.T) {
	if diff := pretty.Compare(testRoutes["test1"], testRoutes.Lookup(net.IP{10, 1, 0, 1})); diff != "" {
		t.Errorf("routes.Lookup 10.1.0.1:\n%s", diff)
	}
	if diff := pretty.Compare(testRoutes["internal"], testRoutes.Lookup(net.IP{10, 99, 0, 1})); diff != "" {
		t.Errorf("routes.Lookup 10.99.0.1:\n%s", diff)
	}
	if diff := pretty.Compare(nil, testRoutes.Lookup(net.IP{192, 0, 2, 1})); diff != "" {
		t.Errorf("routes.Lookup 192.0.2.1:\n%s", diff)
	}
}
Ejemplo n.º 7
0
func TestRevokeRefreshToken(t *testing.T) {
	tests := []struct {
		userID   string
		toRevoke string
		before   []string // clientIDs expected before the change.
		after    []string // clientIDs expected after the change.
	}{
		{"ID-1", goodClientID, []string{goodClientID}, []string{}},
		{"ID-2", goodClientID, []string{goodClientID}, []string{}},
	}

	api, _ := makeTestFixtures(false)

	listClientsWithRefreshTokens := func(creds Creds, userID string) ([]string, error) {
		clients, err := api.ListClientsWithRefreshTokens(creds, userID)
		if err != nil {
			return nil, err
		}
		clientIDs := make([]string, len(clients))
		for i, client := range clients {
			clientIDs[i] = client.ClientID
		}
		sort.Strings(clientIDs)
		return clientIDs, nil
	}

	for i, tt := range tests {
		creds := Creds{User: user.User{ID: tt.userID}}

		gotBefore, err := listClientsWithRefreshTokens(creds, tt.userID)
		if err != nil {
			t.Errorf("case %d: list clients failed: %v", i, err)
		} else {
			if diff := pretty.Compare(tt.before, gotBefore); diff != "" {
				t.Errorf("case %d: before exp!=got: %s", i, diff)
			}
		}

		if err := api.RevokeRefreshTokensForClient(creds, tt.userID, tt.toRevoke); err != nil {
			t.Errorf("case %d: failed to revoke client: %v", i, err)
			continue
		}

		gotAfter, err := listClientsWithRefreshTokens(creds, tt.userID)
		if err != nil {
			t.Errorf("case %d: list clients failed: %v", i, err)
		} else {
			if diff := pretty.Compare(tt.after, gotAfter); diff != "" {
				t.Errorf("case %d: after exp!=got: %s", i, diff)
			}
		}
	}
}
Ejemplo n.º 8
0
func TestMetricToCollectd(t *testing.T) {
	ts, terr := time.Parse("2006/01/02 15:04:05", "2012/07/24 10:14:00")
	if terr != nil {
		t.Errorf("time parse error: %s", terr)
	}
	ms := metrics.NewStore()

	scalarMetric := metrics.NewMetric("foo", "prog", metrics.Counter)
	d, _ := scalarMetric.GetDatum()
	d.Set(37, ts)
	ms.Add(scalarMetric)

	r := FakeSocketWrite(metricToCollectd, scalarMetric)
	expected := []string{"PUTVAL \"gunstar/mtail-prog/counter-foo\" interval=60 1343124840:37\n"}
	diff := pretty.Compare(r, expected)
	if len(diff) > 0 {
		t.Errorf("String didn't match:\n%s", diff)
	}

	dimensionedMetric := metrics.NewMetric("bar", "prog", metrics.Gauge, "label")
	d, _ = dimensionedMetric.GetDatum("quux")
	d.Set(37, ts)
	d, _ = dimensionedMetric.GetDatum("snuh")
	d.Set(37, ts)
	ms.ClearMetrics()
	ms.Add(dimensionedMetric)

	r = FakeSocketWrite(metricToCollectd, dimensionedMetric)
	expected = []string{
		"PUTVAL \"gunstar/mtail-prog/gauge-bar-label-quux\" interval=60 1343124840:37\n",
		"PUTVAL \"gunstar/mtail-prog/gauge-bar-label-snuh\" interval=60 1343124840:37\n"}
	diff = pretty.Compare(r, expected)
	if len(diff) > 0 {
		t.Errorf("String didn't match:\n%s", diff)
	}

	timingMetric := metrics.NewMetric("foo", "prog", metrics.Timer)
	d, _ = timingMetric.GetDatum()
	d.Set(123, ts)
	ms.Add(timingMetric)

	r = FakeSocketWrite(metricToCollectd, timingMetric)
	expected = []string{"PUTVAL \"gunstar/mtail-prog/gauge-foo\" interval=60 1343124840:123\n"}
	diff = pretty.Compare(r, expected)
	if len(diff) > 0 {
		t.Errorf("String didn't match:\n%s", diff)
	}
}
Ejemplo n.º 9
0
func TestOutMessageHeader(t *testing.T) {
	var om OutMessage
	om.Reset()

	// Fill in the header.
	want := fusekernel.OutHeader{
		Len:    0xdeadbeef,
		Error:  -31231917,
		Unique: 0xcafebabeba5eba11,
	}

	h := om.OutHeader()
	if h == nil {
		t.Fatal("OutHeader returned nil")
	}

	*h = want

	// Check that the result is as expected.
	b := om.Bytes()
	if len(b) != int(unsafe.Sizeof(want)) {
		t.Fatalf("unexpected length %d; want %d", len(b), unsafe.Sizeof(want))
	}

	got := *(*fusekernel.OutHeader)(unsafe.Pointer(&b[0]))
	if diff := pretty.Compare(got, want); diff != "" {
		t.Errorf("diff -got +want:\n%s", diff)
	}
}
Ejemplo n.º 10
0
func TestClientMetadataMarshalRoundTrip(t *testing.T) {
	tests := []ClientMetadata{
		{
			RedirectURIs: []url.URL{
				{Scheme: "https", Host: "example.com", Path: "/callback"},
			},
			LogoURI: &url.URL{Scheme: "https", Host: "example.com", Path: "/logo"},
			RequestObjectOptions: JWAOptions{
				EncryptionAlg: "RSA1_5",
				EncryptionEnc: "A128CBC-HS256",
			},
			ApplicationType:         "native",
			TokenEndpointAuthMethod: "client_secret_basic",
		},
	}

	for i, want := range tests {
		data, err := json.Marshal(&want)
		if err != nil {
			t.Errorf("case %d: failed to marshal metadata: %v", i, err)
			continue
		}
		var got ClientMetadata
		if err := json.Unmarshal(data, &got); err != nil {
			t.Errorf("case %d: failed to unmarshal metadata: %v", i, err)
			continue
		}
		if diff := pretty.Compare(want, got); diff != "" {
			t.Errorf("case %d: struct did not survive a marshaling round trip: %s", i, diff)
		}
	}
}
Ejemplo n.º 11
0
func TestTagMapping(t *testing.T) {
	mapper := NewTagMapper("db", "json")
	data := TestMappingStruct{}
	val := reflect.ValueOf(data)
	expected := map[string][]int{
		"NoTag":                        {0},
		"json_tag":                     {1},
		"db_tag":                       {2},
		"both_tag_db":                  {3},
		"String":                       {4},
		"StringPtr":                    {5},
		"Time":                         {6},
		"TimePtr":                      {7},
		"Int":                          {8},
		"IntPtr":                       {9},
		"Float":                        {10},
		"FloatPtr":                     {11},
		"Bool":                         {12},
		"BoolPtr":                      {13},
		"IncludedStruct.IncludedField": {15, 0},
		"IncludedStruct.NoTag":         {15, 1},
		"IncludedStructSingleField":    {16, 0},
	}
	result, err := mapper.KeyIndexes(val)
	if err != nil {
		t.Errorf("Unexpected error: %s", err)
	}
	if !reflect.DeepEqual(result.Indexes, expected) {
		t.Errorf("Unexpected Results\n%s", pretty.Compare(result, expected))

	}
}
Ejemplo n.º 12
0
func TestInhibitRuleGC(t *testing.T) {
	// TODO(fabxc): add now() injection function to Resolved() to remove
	// dependency on machine time in this test.
	now := time.Now()
	newAlert := func(start, end time.Duration) *types.Alert {
		return &types.Alert{
			Alert: model.Alert{
				Labels:   model.LabelSet{"a": "b"},
				StartsAt: now.Add(start * time.Minute),
				EndsAt:   now.Add(end * time.Minute),
			},
		}
	}

	before := map[model.Fingerprint]*types.Alert{
		0: newAlert(-10, -5),
		1: newAlert(10, 20),
		2: newAlert(-10, 10),
		3: newAlert(-10, -1),
	}
	after := map[model.Fingerprint]*types.Alert{
		1: newAlert(10, 20),
		2: newAlert(-10, 10),
	}

	r := &InhibitRule{scache: before}
	r.gc()

	if !reflect.DeepEqual(r.scache, after) {
		t.Errorf("Unexpected cache state after GC")
		t.Errorf(pretty.Compare(r.scache, after))
	}
}
Ejemplo n.º 13
0
func TestEmitLabelSet(t *testing.T) {
	m := NewMetric("test", "prog", Gauge, "foo", "bar", "quux")
	c := make(chan *LabelSet)

	ts := time.Now().UTC()

	var expectedLabels []map[string]string
	for _, tc := range labelSetTests {
		d, _ := m.GetDatum(tc.values...)
		d.Set(37, ts)
		expectedLabels = append(expectedLabels, tc.expectedLabels)
	}

	go m.EmitLabelSets(c)

	var labels []map[string]string
	for ls := range c {
		labels = append(labels, ls.Labels)
	}

	diff := pretty.Compare(labels, expectedLabels)
	if len(diff) > 0 {
		t.Errorf("Labels don't match:\n%s", diff)
	}
}
Ejemplo n.º 14
0
func TestPage(t *testing.T) {
	p := &Page{
		URL:          "https://www.example.com",
		HTML:         testPage,
		MetaReader:   &MetaReader{},
		SchemaReader: &SchemaReader{},
	}
	p.PageReader = &PageReader{page: p}
	mockbytes := []byte(testPage)
	p.Read(mockbytes)
	if p.Title != "TestPage" {
		t.Errorf("Expected page title %s", p.Title)
	}
	if p.Text != "Blend-O-Matic $19.95 Based on 25 user ratings Spinal Tap One of the loudest bands ever reunites for an unforgettable two-day show. Event date: May 8, 7:30pm Alice Jones Bob Smith" {
		t.Errorf("unexpected page text %s", p.Text)
	}
	for i, m := range p.MetaData() {
		if !reflect.DeepEqual(m, meta[i]) {
			t.Errorf(`Meta result item %d differs:
                expected %+v
                got %+v`, i, meta[i], m)
		}
	}
	for i, s := range p.SchemaData() {
		if !reflect.DeepEqual(s, schema[i]) {
			t.Error(pretty.Compare(s, schema[i]))
		}
	}
}
Ejemplo n.º 15
0
func TestParserRoundTrip(t *testing.T) {
	for _, tc := range mtailPrograms {
		p := newParser(tc.name, strings.NewReader(tc.program), &metrics.Store{})
		r := mtailParse(p)

		if r != 0 || p.root == nil || len(p.errors) > 0 {
			t.Errorf("1st pass parse errors:\n")
			for _, e := range p.errors {
				t.Errorf("\t%s\n", e)
			}
			continue
		}

		u := Unparser{}
		output := u.Unparse(p.root)

		p2 := newParser(tc.name+" 2", strings.NewReader(output), &metrics.Store{})
		r = mtailParse(p2)
		if r != 0 || p2.root == nil || len(p2.errors) > 0 {
			t.Errorf("2nd pass parse errors:\n")
			for _, e := range p2.errors {
				t.Errorf("\t%s\n", e)
			}
			continue
		}

		u = Unparser{}
		output2 := u.Unparse(p2.root)

		diff := pretty.Compare(output2, output)
		if len(diff) > 0 {
			t.Errorf("Round trip failed to generate same output.\n%s", diff)
		}
	}
}
Ejemplo n.º 16
0
func TestRefreshTokenRepoClientsWithRefreshTokens(t *testing.T) {
	tests := []struct {
		clientIDs []string
	}{
		{clientIDs: []string{"client1", "client2"}},
		{clientIDs: []string{"client1"}},
		{clientIDs: []string{}},
	}

	for i, tt := range tests {
		repo := newRefreshRepo(t, testRefreshUsers, testRefreshClients)

		for _, clientID := range tt.clientIDs {
			_, err := repo.Create(testRefreshUserID, clientID, testRefreshConnectorID, []string{"openid"})
			if err != nil {
				t.Fatalf("case %d: client_id: %s couldn't create refresh token: %v", i, clientID, err)
			}
		}

		clients, err := repo.ClientsWithRefreshTokens(testRefreshUserID)
		if err != nil {
			t.Fatalf("case %d: unexpected error fetching clients %q", i, err)
		}
		var clientIDs []string
		for _, client := range clients {
			clientIDs = append(clientIDs, client.Credentials.ID)
		}
		sort.Strings(clientIDs)

		if diff := pretty.Compare(clientIDs, tt.clientIDs); diff != "" {
			t.Errorf("case %d: Compare(want, got): %v", i, diff)
		}
	}
}
Ejemplo n.º 17
0
func TestRefreshTokenRepoRevokeForClient(t *testing.T) {
	tests := []struct {
		createIDs []string
		revokeID  string
	}{
		{
			createIDs: []string{"client1", "client2"},
			revokeID:  "client1",
		},
		{
			createIDs: []string{"client2"},
			revokeID:  "client1",
		},
		{
			createIDs: []string{"client1"},
			revokeID:  "client1",
		},
		{
			createIDs: []string{},
			revokeID:  "oops",
		},
	}

	for i, tt := range tests {
		repo := newRefreshRepo(t, testRefreshUsers, testRefreshClients)

		for _, clientID := range tt.createIDs {
			_, err := repo.Create(testRefreshUserID, clientID, testRefreshConnectorID, []string{"openid"})
			if err != nil {
				t.Fatalf("case %d: client_id: %s couldn't create refresh token: %v", i, clientID, err)
			}

			if err := repo.RevokeTokensForClient(testRefreshUserID, tt.revokeID); err != nil {
				t.Fatalf("case %d: couldn't revoke refresh token(s): %v", i, err)
			}
		}

		var wantIDs []string
		for _, id := range tt.createIDs {
			if id != tt.revokeID {
				wantIDs = append(wantIDs, id)
			}
		}

		clients, err := repo.ClientsWithRefreshTokens(testRefreshUserID)
		if err != nil {
			t.Fatalf("case %d: unexpected error fetching clients %q", i, err)
		}

		var gotIDs []string
		for _, client := range clients {
			gotIDs = append(gotIDs, client.Credentials.ID)
		}
		sort.Strings(gotIDs)

		if diff := pretty.Compare(wantIDs, gotIDs); diff != "" {
			t.Errorf("case %d: Compare(wantIDs, gotIDs): %v", i, diff)
		}
	}
}
Ejemplo n.º 18
0
func checkMinimalContainer(t *testing.T, acipath string, expectedManifest schema.ImageManifest) {
	// Check that there are no files in the rootfs
	files, err := ioutil.ReadDir(path.Join(acipath, aci.RootfsDir))
	if err != nil {
		t.Errorf("%v", err)
	}
	if len(files) != 0 {
		t.Errorf("rootfs in aci contains files, should be empty")
	}

	// Check that the manifest is no bigger than it needs to be
	manblob, err := ioutil.ReadFile(path.Join(acipath, aci.ManifestFile))
	if err != nil {
		t.Errorf("%v", err)
	}

	var man schema.ImageManifest

	err = man.UnmarshalJSON(manblob)
	if err != nil {
		t.Errorf("invalid manifest schema: %v", err)
	}

	if str := pretty.Compare(man, expectedManifest); str != "" {
		t.Errorf("unexpected manifest:\n%s", str)
	}
}
Ejemplo n.º 19
0
func TestHandlePrometheus(t *testing.T) {
	for _, tc := range handlePrometheusTests {
		ms := metrics.NewStore()
		for _, metric := range tc.metrics {
			ms.Add(metric)
		}
		o := Options{ms, "gunstar"}
		e, err := New(o)
		if err != nil {
			t.Fatalf("couldn't make exporter: %s", err)
		}
		response := httptest.NewRecorder()
		e.HandlePrometheusMetrics(response, &http.Request{})
		if response.Code != 200 {
			t.Errorf("test case %s: response code not 200: %d", tc.name, response.Code)
		}
		b, err := ioutil.ReadAll(response.Body)
		if err != nil {
			t.Errorf("test case %s: failed to read response: %s", tc.name, err)
		}
		diff := pretty.Compare(string(b), tc.expected)
		if len(diff) > 0 {
			t.Errorf("test case %s: response not expected:\n%s", tc.name, diff)
		}
	}
}
Ejemplo n.º 20
0
func runSteps(t *testing.T, singleton bool, steps []step) {
	logOut := &bytes.Buffer{}
	logOut.WriteByte('\n')
	logW := &iotool.SafeWriter{W: logOut}
	defer func() {
		logW.SetWriter(nil) // disable writes to avoid races
		t.Log(logOut)
	}()

	dataOut := &bytes.Buffer{}
	sim := simulator.New(singleton, dataOut, logW)
	defer sim.Close()

	for _, step := range steps {
		if step.JSON != nil {
			jsonData, _ := json.Marshal(step.JSON)
			step.Cmd = fmt.Sprintf("%s %s", step.Cmd, string(jsonData))
		}

		fmt.Fprintf(logW, "== %s\n", step.Cmd)
		sim.RunCommand(step.Cmd)
		if step.Check != nil {
			actual := reflect.New(reflect.Indirect(reflect.ValueOf(step.Check)).Type()).Interface()
			if err := json.Unmarshal(dataOut.Bytes(), actual); err != nil {
				t.Fatal("json decode error", err)
			}
			if diff := pretty.Compare(step.Check, actual); diff != "" {
				t.Fatalf("check failed:\n%s", diff)
			}
		}
		dataOut.Reset()
	}
}
Ejemplo n.º 21
0
func testRequests(cases []testCase, t *testing.T) {
	c := &mockClient{}
	r := &reaperImpl{
		cli:        c,
		parser:     &mockParser{},
		hostname:   "reddit.com",
		reapSuffix: ".json",
		scheme:     "https",
		mu:         &sync.Mutex{},
	}
	b := &bot{
		Account: newAccount(r),
		Lurker:  newLurker(r),
		Scanner: newScanner(r),
	}
	for _, test := range cases {
		if err := test.f(b); err != test.err {
			t.Errorf("[%s] unexpected error: %v", test.name, err)
		}

		if diff := pretty.Compare(c.request, test.correct); diff != "" {
			t.Errorf(
				"[%s] request incorrect; diff: %s",
				test.name,
				diff,
			)
		}
	}
}
Ejemplo n.º 22
0
func TestParse_Medications(t *testing.T) {
	c := ccd.NewDefaultCCD()
	err := parseAndRecover(t, c, "testdata/specific/medications.xml", nil)
	if err != nil {
		t.Fatal(err)
	}

	meds := []ccd.Medication{
		ccd.Medication{
			Name:           "Albuterol 0.09 MG/ACTUAT inhalant solution",
			Administration: "",
			Dose: ccd.MedicationDose{
				LowValue:  "0.09",
				LowUnit:   "mg/actuat",
				HighValue: "",
				HighUnit:  "",
			},
			Status:     "Active",
			StatusCode: "completed",
			StartDate:  time.Time{},
			StopDate:   time.Date(2012, 8, 6, 0, 0, 0, 0, time.UTC),
			Period:     time.Duration(43200000000000),
			Code: ccd.Code{
				CodeSystemName: "",
				Type:           "",
				CodeSystem:     "2.16.840.1.113883.6.88",
				Code:           "573621",
				DisplayName:    "Albuterol 0.09 MG/ACTUAT inhalant solution",
				Translations: []ccd.Code{ccd.Code{
					CodeSystemName: "RxNorm",
					Type:           "",
					CodeSystem:     "2.16.840.1.113883.6.88",
					Code:           "573621",
					DisplayName:    "Proventil 0.09 MG/ACTUAT inhalant solution",
					OriginalText:   "",
				}},
			},
			Reason: &ccd.MedicationReason{
				Value: ccd.Code{
					CodeSystemName: "",
					Type:           "CD",
					CodeSystem:     "2.16.840.1.113883.6.96",
					Code:           "233604007",
					DisplayName:    "Pneumonia",
				},
				Date: time.Time{},
			},
		},
	}

	if len(meds) != len(c.Medications) {
		t.Fatalf("Expected %d medications. Got %d", len(meds), len(c.Medications))
	}

	for i, _ := range meds {
		if !reflect.DeepEqual(meds[i], c.Medications[i]) {
			t.Fatalf("Differences in medication %d: %v", i, pretty.Compare(meds[i], c.Medications[i]))
		}
	}
}
Ejemplo n.º 23
0
func TestDBClientRepoMetadata(t *testing.T) {
	r := db.NewClientRepo(connect(t))

	cm := oidc.ClientMetadata{
		RedirectURIs: []url.URL{
			url.URL{Scheme: "http", Host: "127.0.0.1:5556", Path: "/cb"},
			url.URL{Scheme: "https", Host: "example.com", Path: "/callback"},
		},
	}

	_, err := r.New(nil, client.Client{
		Credentials: oidc.ClientCredentials{
			ID: "foo",
		},
		Metadata: cm,
	})
	if err != nil {
		t.Fatalf(err.Error())
	}

	got, err := r.Get(nil, "foo")
	if err != nil {
		t.Fatalf(err.Error())
	}

	if diff := pretty.Compare(cm, got.Metadata); diff != "" {
		t.Fatalf("Retrieved incorrect ClientMetadata: Compare(want,got): %v", diff)
	}
}
Ejemplo n.º 24
0
func TestEncodeDecodeNextPageToken(t *testing.T) {
	tests := []nextPageToken{
		{},
		{MaxResults: 100},
		{Offset: 200},
		{MaxResults: 20, Offset: 30},
	}

	for i, tt := range tests {
		enc, err := EncodeNextPageToken(tt.Filter, tt.MaxResults, tt.Offset)
		if err != nil {
			t.Errorf("case %d: unexpected err encoding: %q", i, err)
		}

		dec := nextPageToken{}
		dec.Filter, dec.MaxResults, dec.Offset, err = DecodeNextPageToken(enc)
		if err != nil {
			t.Errorf("case %d: unexpected err decoding: %q", i, err)
		}

		if diff := pretty.Compare(tt, dec); diff != "" {
			t.Errorf("case %d: Compare(want, got): %v", i, diff)
		}
	}
}
Ejemplo n.º 25
0
func TestGetOrCreateCategoryPath(t *testing.T) {
	db := mustDB(t)
	path, err := db.CategoryPath([]string{"a", "b", "c"}, true)
	if err != nil {
		t.Fatal(err)
	}
	wantPath := CategoryPath{
		&Category{ID: path[0].ID, Name: "a"},
		&Category{ID: path[1].ID, Name: "b", ParentID: path[0].ID},
		&Category{ID: path[2].ID, Name: "c", ParentID: path[1].ID},
	}
	if diff := pretty.Compare(path, wantPath); diff != "" {
		t.Fatal(diff)
	} else if path2, err := db.CategoryPath([]string{"a", "b", "c"}, true); err != nil {
		t.Fatal(err)
	} else if diff := pretty.Compare(path2, wantPath); diff != "" {
		t.Fatal(diff)
	}
	path3, err := db.CategoryPath([]string{"a", "d"}, true)
	if err != nil {
		t.Fatal(err)
	}
	wantPath = CategoryPath{
		&Category{ID: path[0].ID, Name: "a"},
		&Category{ID: path3[1].ID, Name: "d", ParentID: path[0].ID},
	}
	if diff := pretty.Compare(path3, wantPath); diff != "" {
		t.Fatal(diff)
	}
	categories, err := db.Categories()
	if err != nil {
		t.Fatal(err)
	}
	wantCategories := CategoryMap{
		path[0].ID:  path[0],
		path[1].ID:  path[1],
		path[2].ID:  path[2],
		path3[1].ID: path3[1],
	}
	if diff := pretty.Compare(categories, wantCategories); diff != "" {
		t.Fatal(diff)
	}
	// @TODO check actual error
	if _, err := db.CategoryPath([]string{"a", "e"}, false); err == nil {
		t.Fatal("expected does not exist error")
	}
}
Ejemplo n.º 26
0
func TestRefreshTokenRepo(t *testing.T) {
	clientID := "client1"
	userID := "user1"
	clients := []client.Client{
		{
			Credentials: oidc.ClientCredentials{
				ID:     clientID,
				Secret: base64.URLEncoding.EncodeToString([]byte("secret-2")),
			},
			Metadata: oidc.ClientMetadata{
				RedirectURIs: []url.URL{
					url.URL{Scheme: "https", Host: "client1.example.com", Path: "/callback"},
				},
			},
		},
	}
	users := []user.UserWithRemoteIdentities{
		{
			User: user.User{
				ID:        userID,
				Email:     "*****@*****.**",
				CreatedAt: time.Now().Truncate(time.Second),
			},
			RemoteIdentities: []user.RemoteIdentity{
				{
					ConnectorID: "IDPC-1",
					ID:          "RID-1",
				},
			},
		},
	}

	repo := newRefreshRepo(t, users, clients)
	tok, err := repo.Create(userID, clientID)
	if err != nil {
		t.Fatalf("failed to create refresh token: %v", err)
	}
	if tokUserID, err := repo.Verify(clientID, tok); err != nil {
		t.Errorf("Could not verify token: %v", err)
	} else if tokUserID != userID {
		t.Errorf("Verified token returned wrong user id, want=%s, got=%s", userID, tokUserID)
	}

	if userClients, err := repo.ClientsWithRefreshTokens(userID); err != nil {
		t.Errorf("Failed to get the list of clients the user was logged into: %v", err)
	} else {
		if diff := pretty.Compare(userClients, clients); diff == "" {
			t.Errorf("Clients user logged into: want did not equal got %s", diff)
		}
	}

	if err := repo.RevokeTokensForClient(userID, clientID); err != nil {
		t.Errorf("Failed to revoke refresh token: %v", err)
	}

	if _, err := repo.Verify(clientID, tok); err == nil {
		t.Errorf("Token which should have been revoked was verified")
	}
}
Ejemplo n.º 27
0
func TestSow(t *testing.T) {
	for i, test := range []struct {
		path    string
		values  map[string]string
		correct http.Request
	}{
		{"", nil, http.Request{
			Method: "POST",
			Header: formEncoding,
			Host:   "com",
			URL: &url.URL{
				Scheme:   "http",
				Host:     "com",
				Path:     "",
				RawQuery: "",
			},
		}},
		{"", map[string]string{"key": "value"}, http.Request{
			Method: "POST",
			Header: formEncoding,
			Host:   "com",
			URL: &url.URL{
				Scheme:   "http",
				Host:     "com",
				Path:     "",
				RawQuery: "key=value",
			},
		}},
		{"path", nil, http.Request{
			Method: "POST",
			Header: formEncoding,
			Host:   "com",
			URL: &url.URL{
				Scheme:   "http",
				Host:     "com",
				Path:     "path",
				RawQuery: "",
			},
		}},
	} {
		c := &mockClient{}
		r := &reaperImpl{
			cli:      c,
			parser:   &mockParser{},
			hostname: "com",
			scheme:   "http",
			mu:       &sync.Mutex{},
		}

		if err := r.sow(test.path, test.values); err != nil {
			t.Errorf("Error reaping input %d: %v", i, err)
		}

		if diff := pretty.Compare(c.request, test.correct); diff != "" {
			t.Errorf("request incorrect; diff: %s", diff)
		}
	}
}
Ejemplo n.º 28
0
func TestExamplePrograms(t *testing.T) {
	if testing.Short() {
		t.Skip("skipping test in short mode")
	}
	for _, tc := range exampleProgramTests {
		w := watcher.NewFakeWatcher()
		store := &metrics.Store{}
		o := mtail.Options{Progs: tc.programfile, W: w, Store: store}
		mtail, err := mtail.New(o)
		if err != nil {
			t.Fatalf("create mtail failed: %s", err)
		}

		if err := mtail.OneShot(tc.logfile); err != nil {
			t.Errorf("Oneshot failed for %s: %s", tc.logfile, err)
			continue
		}

		// Dirty hack to create json files :)
		if false {
			j, err := os.Create(tc.jsonfile)
			if err != nil {
				t.Errorf("%s: could not open json file: %s", tc.jsonfile, err)
				continue
			}
			if err := mtail.WriteMetrics(j); err != nil {
				t.Errorf("couldn't marshall metrics: %q", err)
				continue
			}
			j.Close()
		}

		j, err := os.Open(tc.jsonfile)
		if err != nil {
			t.Fatalf("%s: could not open json file: %s", tc.jsonfile, err)
		}
		defer j.Close()
		var ex bytes.Buffer

		if _, err := ex.ReadFrom(j); err != nil {
			t.Fatalf("Couldn't read from json: %s", err)
		}

		mtail.Close()
		var m bytes.Buffer
		mtail.WriteMetrics(&m)
		m.WriteString("\n") // Golden data has trailing newline.

		diff := pretty.Compare(
			strings.Split(ex.String(), "\n"), // want
			strings.Split(m.String(), "\n"))  // got
		if len(diff) > 0 {
			t.Errorf("%s: metrics don't match:\n%s", tc.programfile, diff)

			t.Errorf("Store metrics: %#v", store.Metrics)
		}
	}
}
Ejemplo n.º 29
0
func TestLex(t *testing.T) {
	for _, test := range lexerTests {
		tokens := collect(&test)
		diff := pretty.Compare(test.tokens, tokens)
		if len(diff) > 0 {
			t.Errorf("%s tokens didn't match:\n%s:", test.name, diff)
		}
	}
}
Ejemplo n.º 30
0
func TestDBClientAll(t *testing.T) {
	r := db.NewClientRepo(connect(t))

	cm := oidc.ClientMetadata{
		RedirectURIs: []url.URL{
			url.URL{Scheme: "http", Host: "127.0.0.1:5556", Path: "/cb"},
		},
	}

	_, err := r.New(nil, client.Client{
		Credentials: oidc.ClientCredentials{
			ID: "foo",
		},
		Metadata: cm,
	})
	if err != nil {
		t.Fatalf(err.Error())
	}

	got, err := r.All(nil)
	if err != nil {
		t.Fatalf(err.Error())
	}
	count := len(got)
	if count != 1 {
		t.Fatalf("Retrieved incorrect number of ClientIdentities: want=1 got=%d", count)
	}

	if diff := pretty.Compare(cm, got[0].Metadata); diff != "" {
		t.Fatalf("Retrieved incorrect ClientMetadata: Compare(want,got): %v", diff)
	}

	cm = oidc.ClientMetadata{
		RedirectURIs: []url.URL{
			url.URL{Scheme: "http", Host: "foo.com", Path: "/cb"},
		},
	}
	_, err = r.New(nil, client.Client{
		Credentials: oidc.ClientCredentials{
			ID: "bar",
		},
		Metadata: cm,
	})
	if err != nil {
		t.Fatalf(err.Error())
	}

	got, err = r.All(nil)
	if err != nil {
		t.Fatalf(err.Error())
	}
	count = len(got)
	if count != 2 {
		t.Fatalf("Retrieved incorrect number of ClientIdentities: want=2 got=%d", count)
	}
}