|
800 | 800 | end |
801 | 801 | end |
802 | 802 | end |
| 803 | + |
| 804 | + describe "fetch_blocked_versions" do |
| 805 | + let(:blocked_versions_url) { "http://example.com/update_jobs/1/blocked_versions" } |
| 806 | + |
| 807 | + context "when the API returns blocked versions" do |
| 808 | + before do |
| 809 | + stub_request(:get, blocked_versions_url) |
| 810 | + .with(query: { "package-manager": "npm_and_yarn" }) |
| 811 | + .to_return( |
| 812 | + status: 200, |
| 813 | + body: { |
| 814 | + data: [ |
| 815 | + { "dependency-name" => "event-stream", "version-requirement" => "= 3.3.6", "reason" => "malware" }, |
| 816 | + { "dependency-name" => "flatmap-stream", "version-requirement" => "= 0.1.1", "reason" => "malware" } |
| 817 | + ] |
| 818 | + }.to_json, |
| 819 | + headers: headers |
| 820 | + ) |
| 821 | + end |
| 822 | + |
| 823 | + it "returns the blocked versions array" do |
| 824 | + result = client.fetch_blocked_versions("npm_and_yarn") |
| 825 | + expect(result).to eq( |
| 826 | + [ |
| 827 | + { "dependency-name" => "event-stream", "version-requirement" => "= 3.3.6", "reason" => "malware" }, |
| 828 | + { "dependency-name" => "flatmap-stream", "version-requirement" => "= 0.1.1", "reason" => "malware" } |
| 829 | + ] |
| 830 | + ) |
| 831 | + end |
| 832 | + end |
| 833 | + |
| 834 | + context "when the API returns an error" do |
| 835 | + before do |
| 836 | + stub_request(:get, blocked_versions_url) |
| 837 | + .with(query: { "package-manager": "npm_and_yarn" }) |
| 838 | + .to_return(status: 500, body: "Internal Server Error", headers: headers) |
| 839 | + end |
| 840 | + |
| 841 | + it "returns an empty array and logs a warning" do |
| 842 | + expect(Dependabot.logger).to receive(:warn).with(/Failed to fetch blocked versions/) |
| 843 | + result = client.fetch_blocked_versions("npm_and_yarn") |
| 844 | + expect(result).to eq([]) |
| 845 | + end |
| 846 | + end |
| 847 | + |
| 848 | + context "when the API times out" do |
| 849 | + before do |
| 850 | + stub_request(:get, blocked_versions_url) |
| 851 | + .with(query: { "package-manager": "npm_and_yarn" }) |
| 852 | + .to_timeout |
| 853 | + end |
| 854 | + |
| 855 | + it "returns an empty array and logs a warning" do |
| 856 | + expect(Dependabot.logger).to receive(:warn).with(/Failed to fetch blocked versions/) |
| 857 | + result = client.fetch_blocked_versions("npm_and_yarn") |
| 858 | + expect(result).to eq([]) |
| 859 | + end |
| 860 | + end |
| 861 | + |
| 862 | + context "when the API returns no blocked versions" do |
| 863 | + before do |
| 864 | + stub_request(:get, blocked_versions_url) |
| 865 | + .with(query: { "package-manager": "npm_and_yarn" }) |
| 866 | + .to_return( |
| 867 | + status: 200, |
| 868 | + body: { data: [] }.to_json, |
| 869 | + headers: headers |
| 870 | + ) |
| 871 | + end |
| 872 | + |
| 873 | + it "returns an empty array" do |
| 874 | + result = client.fetch_blocked_versions("npm_and_yarn") |
| 875 | + expect(result).to eq([]) |
| 876 | + end |
| 877 | + end |
| 878 | + |
| 879 | + context "when the API returns invalid JSON" do |
| 880 | + before do |
| 881 | + stub_request(:get, blocked_versions_url) |
| 882 | + .with(query: { "package-manager": "npm_and_yarn" }) |
| 883 | + .to_return(status: 200, body: "not json", headers: headers) |
| 884 | + end |
| 885 | + |
| 886 | + it "returns an empty array and logs a warning" do |
| 887 | + expect(Dependabot.logger).to receive(:warn).with(/Failed to parse blocked versions/) |
| 888 | + result = client.fetch_blocked_versions("npm_and_yarn") |
| 889 | + expect(result).to eq([]) |
| 890 | + end |
| 891 | + end |
| 892 | + |
| 893 | + context "when the API returns data that is not an array" do |
| 894 | + before do |
| 895 | + stub_request(:get, blocked_versions_url) |
| 896 | + .with(query: { "package-manager": "npm_and_yarn" }) |
| 897 | + .to_return( |
| 898 | + status: 200, |
| 899 | + body: { data: "unexpected" }.to_json, |
| 900 | + headers: headers |
| 901 | + ) |
| 902 | + end |
| 903 | + |
| 904 | + it "returns an empty array and logs a warning" do |
| 905 | + expect(Dependabot.logger).to receive(:warn).with(/Unexpected blocked versions format/) |
| 906 | + result = client.fetch_blocked_versions("npm_and_yarn") |
| 907 | + expect(result).to eq([]) |
| 908 | + end |
| 909 | + end |
| 910 | + |
| 911 | + context "when the API returns a non-object JSON body" do |
| 912 | + before do |
| 913 | + stub_request(:get, blocked_versions_url) |
| 914 | + .with(query: { "package-manager": "npm_and_yarn" }) |
| 915 | + .to_return(status: 200, body: "[]", headers: headers) |
| 916 | + end |
| 917 | + |
| 918 | + it "returns an empty array and logs a warning" do |
| 919 | + expect(Dependabot.logger).to receive(:warn).with(/Unexpected blocked versions format/) |
| 920 | + result = client.fetch_blocked_versions("npm_and_yarn") |
| 921 | + expect(result).to eq([]) |
| 922 | + end |
| 923 | + end |
| 924 | + |
| 925 | + context "when the API returns data entries that are not hashes" do |
| 926 | + before do |
| 927 | + stub_request(:get, blocked_versions_url) |
| 928 | + .with(query: { "package-manager": "npm_and_yarn" }) |
| 929 | + .to_return( |
| 930 | + status: 200, |
| 931 | + body: { data: [1, "not-a-hash"] }.to_json, |
| 932 | + headers: headers |
| 933 | + ) |
| 934 | + end |
| 935 | + |
| 936 | + it "returns an empty array and logs a warning" do |
| 937 | + expect(Dependabot.logger).to receive(:warn).with(/Unexpected blocked versions format/) |
| 938 | + result = client.fetch_blocked_versions("npm_and_yarn") |
| 939 | + expect(result).to eq([]) |
| 940 | + end |
| 941 | + end |
| 942 | + end |
803 | 943 | end |
0 commit comments