Skip to content

Commit

Permalink
Add missing 'basic' to basic auth
Browse files Browse the repository at this point in the history
Fix #33 by adding the missing 'Basic'
  • Loading branch information
derjust committed Nov 24, 2020
1 parent 8c860bd commit 6ba38a0
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 50 deletions.
2 changes: 1 addition & 1 deletion dist/module.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/module.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
}
],
"version": "7.0.22",
"updated": "2020-11-21"
"updated": "2020-11-24"
},
"dependencies": {
"grafanaVersion": "7.x.x",
Expand Down
81 changes: 44 additions & 37 deletions src/ButtonPanel.test.tsx
Original file line number Diff line number Diff line change
@@ -1,43 +1,50 @@
import { ButtonPanel } from './ButtonPanel';
import { ButtonPanelOptions } from './types';

class TestProps {
options: ButtonPanelOptions = {
url: '',
type: 'header',
contentType: '',
isAuth: false,
params: [],
text: '',
variant: 'primary',
orientation: '',
};
}

describe('ButtonPanel', () => {

let underTest : ButtonPanel;
let props: any;

beforeEach(() => {
let options : ButtonPanelOptions = {
url: '',
type: '',
contentType: '',
isAuth: false,
params: [],
text: '',
variant: 'primary',
orientation: '',

};
props = {
options
};
underTest = new ButtonPanel(props);
});

it('should set a proper orientation', () => {

expect(underTest.getOrientation()).toBe('center');

props.options.orientation = 'left';
expect(underTest.getOrientation()).toBe('left');

props.options.orientation = 'right';
expect(underTest.getOrientation()).toBe('right');

props.options.orientation = 'center';
expect(underTest.getOrientation()).toBe('center');

});
let underTest: ButtonPanel;
let props = new TestProps();

beforeEach(() => {
props = new TestProps();
underTest = new ButtonPanel(props);
});

it('should set a proper orientation', () => {
expect(underTest.getOrientation()).toBe('center');

props.options.orientation = 'left';
expect(underTest.getOrientation()).toBe('left');

props.options.orientation = 'right';
expect(underTest.getOrientation()).toBe('right');

props.options.orientation = 'center';
expect(underTest.getOrientation()).toBe('center');
});

it('should send a proper basic auth header', () => {
let url = new URL('http://example.com');

props.options.isAuth = true;
props.options.username = 'username';
props.options.password = 'password';

let actual = underTest.prepareFetchOpts(url);
expect(actual).toBeDefined();
expect((actual.headers as Headers).get('authorization')).toBe('Basic dXNlcm5hbWU6cGFzc3dvcmQ=');
});
});
16 changes: 8 additions & 8 deletions src/ButtonPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import "isomorphic-fetch";
import 'isomorphic-fetch';
import React, { PureComponent } from 'react';
import { Button, IconName, ButtonVariant } from '@grafana/ui';
import { PanelProps } from '@grafana/data';
Expand Down Expand Up @@ -30,7 +30,7 @@ export class ButtonPanel extends PureComponent<Props, ButtonPanelState> {
return 'exclamation-triangle';
case 'READY':
default:
return this.props.options.icon;// options.icon;
return this.props.options.icon;
}
}

Expand All @@ -41,7 +41,7 @@ export class ButtonPanel extends PureComponent<Props, ButtonPanelState> {
default:
return '';
}
};
}

getOrientation() {
if (!this.props.options.orientation) {
Expand All @@ -56,7 +56,7 @@ export class ButtonPanel extends PureComponent<Props, ButtonPanelState> {
default:
return 'center';
}
};
}

customStyle() {
if (this.props.options.variant === 'custom') {
Expand All @@ -73,7 +73,7 @@ export class ButtonPanel extends PureComponent<Props, ButtonPanelState> {
}
}

variant() : ButtonVariant | undefined {
variant(): ButtonVariant | undefined {
if (this.props.options.variant === 'custom') {
return undefined;
} else {
Expand All @@ -85,11 +85,11 @@ export class ButtonPanel extends PureComponent<Props, ButtonPanelState> {
return this.interpolateVariables(this.props.options.text);
}

interpolateVariables(text: string){
interpolateVariables(text: string) {
return getTemplateSrv().replace(text, this.props.data.request?.scopedVars);
}

prepareFetchOpts(url: URL) : RequestInit {
prepareFetchOpts(url: URL): RequestInit {
const { options } = this.props;

const requestHeaders: HeadersInit = new Headers();
Expand All @@ -113,7 +113,7 @@ export class ButtonPanel extends PureComponent<Props, ButtonPanelState> {
}

if (options.isAuth) {
requestHeaders.set('Authorization', btoa(options.username + ':' + options.password));
requestHeaders.set('Authorization', 'Basic ' + btoa(options.username + ':' + options.password));
}

if (options.params) {
Expand Down
4 changes: 2 additions & 2 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export const plugin = new PanelPlugin<ButtonPanelOptions>(ButtonPanel).setPanelO
settings: {
disableNamedColors: true,
},
showIf: config => config.variant == 'custom',
showIf: config => config.variant === 'custom',
})
.addColorPicker({
path: 'backgroundColor',
Expand All @@ -127,7 +127,7 @@ export const plugin = new PanelPlugin<ButtonPanelOptions>(ButtonPanel).setPanelO
settings: {
disableNamedColors: true,
},
showIf: config => config.variant == 'custom',
showIf: config => config.variant === 'custom',
})
.addRadio({
path: 'orientation',
Expand Down

0 comments on commit 6ba38a0

Please sign in to comment.