Browse Source

feat(import): working on unit tests for import ctrl

Torkel Ödegaard 9 years ago
parent
commit
540def2c39

+ 3 - 0
conf/defaults.ini

@@ -335,3 +335,6 @@ global_api_key = -1
 
 # global limit on number of logged in users.
 global_session = -1
+
+[grafana_net]
+url = https://grafana.net

+ 2 - 1
pkg/api/gnetproxy.go

@@ -8,6 +8,7 @@ import (
 	"time"
 
 	"github.com/grafana/grafana/pkg/middleware"
+	"github.com/grafana/grafana/pkg/setting"
 	"github.com/grafana/grafana/pkg/util"
 )
 
@@ -27,7 +28,7 @@ func ReverseProxyGnetReq(proxyPath string) *httputil.ReverseProxy {
 		req.URL.Host = "grafana.net"
 		req.Host = "grafana.net"
 
-		req.URL.Path = util.JoinUrlFragments("https://grafana.net/api", proxyPath)
+		req.URL.Path = util.JoinUrlFragments(setting.GrafanaNetUrl+"/api", proxyPath)
 
 		// clear cookie headers
 		req.Header.Del("Cookie")

+ 5 - 0
pkg/setting/setting.go

@@ -138,6 +138,9 @@ var (
 
 	// QUOTA
 	Quota QuotaSettings
+
+	// Grafana.NET URL
+	GrafanaNetUrl string
 )
 
 type CommandLineArgs struct {
@@ -494,6 +497,8 @@ func NewConfigContext(args *CommandLineArgs) error {
 		log.Warn("require_email_validation is enabled but smpt is disabled")
 	}
 
+	GrafanaNetUrl = Cfg.Section("grafana.net").Key("url").MustString("https://grafana.net")
+
 	return nil
 }
 

+ 15 - 1
public/app/features/dashboard/import/import.html → public/app/features/dashboard/import/dash_import.html

@@ -18,7 +18,21 @@
 				<dash-upload on-upload="ctrl.onUpload(dash)"></dash-upload>
 			</form>
 
-			<h5 class="section-heading">Or paste JSON:</h5>
+			<h5 class="section-heading">Grafana.net Dashboard</h5>
+
+      <div class="gf-form-group">
+				<div class="gf-form">
+					<input type="text" class="gf-form-input" ng-ctrl="ctrl.grafanaNetUrl" placeholder="Paste Grafana.net dashboard url or id" ng-change="ctrl.checkGnetDashboard()"></textarea>
+				</div>
+        <div class="gf-form" ng-if="ctrl.gnetError">
+          <label class="gf-form-label text-warning">
+            <i class="fa fa-warning"></i>
+            {{ctrl.gnetError}}
+          </label>
+        </div>
+      </div>
+
+      <h5 class="section-heading">Or paste JSON</h5>
 
 			<div class="gf-form-group">
 				<div class="gf-form">

+ 1 - 1
public/app/features/dashboard/import/import.ts → public/app/features/dashboard/import/dash_import.ts

@@ -126,7 +126,7 @@ export class DashImportCtrl {
 export function dashImportDirective() {
   return {
     restrict: 'E',
-    templateUrl: 'public/app/features/dashboard/import/import.html',
+    templateUrl: 'public/app/features/dashboard/import/dash_import.html',
     controller: DashImportCtrl,
     bindToController: true,
     controllerAs: 'ctrl',

+ 50 - 0
public/app/features/dashboard/specs/dash_import_ctrl_specs.ts

@@ -0,0 +1,50 @@
+import {describe, beforeEach, it, sinon, expect, angularMocks} from 'test/lib/common';
+
+import {DashImportCtrl} from 'app/features/dashboard/import/dash_import';
+import config from 'app/core/config';
+
+describe('DashImportCtrl', function() {
+  var ctx: any = {};
+  var backendSrv = {
+    search: sinon.stub().returns(Promise.resolve([])),
+  };
+
+  beforeEach(angularMocks.module('grafana.core'));
+
+  beforeEach(angularMocks.inject(($rootScope, $controller, $q) => {
+    ctx.$q = $q;
+    ctx.scope = $rootScope.$new();
+    ctx.ctrl = $controller(DashImportCtrl, {
+      $scope: ctx.scope,
+      backendSrv: backendSrv,
+    });
+  }));
+
+  describe('when upload json', function() {
+    beforeEach(function() {
+      config.datasources = {
+        ds: {
+          type: 'test-db',
+        }
+      };
+
+      ctx.ctrl.onUpload({
+        '__inputs': [
+          {name: 'ds', pluginId: 'test-db', type: 'datasource', pluginName: 'Test DB'}
+        ]
+      });
+    });
+
+    it('should build input model', function() {
+      expect(ctx.ctrl.inputs.length).to.eql(1);
+      expect(ctx.ctrl.inputs[0].label).to.eql(1);
+    });
+
+    it('should set inputValid to false', function() {
+      expect(ctx.ctrl.inputsValid).to.eql(false);
+    });
+
+  });
+});
+
+

+ 1 - 1
public/app/features/dashboard/specs/exporter_specs.ts

@@ -4,7 +4,7 @@ import _ from 'lodash';
 import config from 'app/core/config';
 import {DashboardExporter} from '../export/exporter';
 
-describe.only('given dashboard with repeated panels', function() {
+describe('given dashboard with repeated panels', function() {
   var dash, exported;
 
   beforeEach(done => {